Home  >  Q&A  >  body text

javascript - About vue2 triggering click event

Now I have some li's that are bound to the click event @click="a()" and the id of each li is different. Now I want to trigger the @click event of the li with a certain id when I want to enter the page. May I ask how to write .click() using jQuery but it has no effect? ​​
For example
For example, the IDs of li are 1 2 3 4 5. I want to enter the page and trigger the @click event of li with ID 5. This can be solved using jQuery. Can also

为情所困为情所困2711 days ago479

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-05-18 10:54:52

    @click="click ($index)"

    reply
    0
  • PHP中文网

    PHP中文网2017-05-18 10:54:52

    <!DOCTYPE html>
    <html lang="en">
    <head>

    <meta charset="UTF-8" />
    <title>Document</title>

    </head>
    <body>

    <p id="app">

    <ul>

        <li @click="add1()"></li>
        <li @click="add2()"></li>
        <li @click="add3()"></li>
        <li @click="add4()"></li>
        <li @click="add5()"></li>
    </ul>
    </p>
    
    <script src="https://cdn.bootcss.com/vue/2.3.3/vue.js"></script>
    <script>
       var vm=new Vue({
            el:"#app",
            methods:{
                add1(){
                    alert(1);
                }
            }
        });
        window.onload=function(){
             vm.add1();
        };
    </script>

    </body>
    </html>

    是这个意思?

    reply
    0
  • 高洛峰

    高洛峰2017-05-18 10:54:52

    The trigger of this dom is directly inside mounted

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-18 10:54:52

    <template>
        <ul>
            <li v-for="item in list" @click="handleClick(item.id)"></li>
        </ul
    </template>
    
    <script>
        export default {
            data() {
                return {
                    list: [{id:1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}]
                }
            },
            
            mounted() {
                this.handleClick(5)
            },
            
            methods: {
                handleClick(id) {
                    // 执行操作
                }
            }
        }
    </script>

    reply
    0
  • Cancelreply