最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js handle multiple click event - Stack Overflow

programmeradmin5浏览0评论

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?

Share Improve this question asked Mar 7, 2017 at 10:08 pexea12pexea12 1,2092 gold badges19 silver badges36 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

You 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)
}
发布评论

评论列表(0)

  1. 暂无评论