admin管理员组文章数量:1389897
I have a list and want to handle a click event for each item in the list
<ul>
<li
v-for="item, index in items"
:key="index"
@click="select(item)"
>
{{ item }}
</li>
</ul>
And the script is
...
methods: {
select(item) {
console.log('Select', item)
}
}
This works well when there are about 10 items. However, when there are about 1000 items, the performance bees very slow because I attach 1000 events for 1000 items.
The solution is to attach only one click event for the list and use event.target
<ul @click="select($event)">
<li
v-for="item, index in items"
:key="index"
>
{{ item }}
</li>
</ul>
In function select
, how can I get item
corresponding to each item?
I have a list and want to handle a click event for each item in the list
<ul>
<li
v-for="item, index in items"
:key="index"
@click="select(item)"
>
{{ item }}
</li>
</ul>
And the script is
...
methods: {
select(item) {
console.log('Select', item)
}
}
This works well when there are about 10 items. However, when there are about 1000 items, the performance bees very slow because I attach 1000 events for 1000 items.
The solution is to attach only one click event for the list and use event.target
<ul @click="select($event)">
<li
v-for="item, index in items"
:key="index"
>
{{ item }}
</li>
</ul>
In function select
, how can I get item
corresponding to each item?
1 Answer
Reset to default 10You can use
<ul @click="select($event)">
<li
v-for="item, index in items"
:key="index"
:id="index"
>
{{ item }}
</li>
</ul>
Then in your select
:
select($event) {
console.log('Select ', $event.srcElement.id)
}
本文标签: javascriptVuejs handle multiple click eventStack Overflow
版权声明:本文标题:javascript - Vue.js handle multiple click event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744571812a2613385.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论