el-select多选下获取点击的单个dom元素
时间: 2023-10-14 15:24:21 浏览: 55
可以使用 el-select 的 @click.native 事件来监听下拉菜单中每个选的点击事件,然后在事件处理函数中获取点击的单个 dom 元素。具体实现方法如下:
1. 给 el-select 组件绑定 @click.native 事件,并在事件处理函数中获取下拉菜单的所有选项元素:
```html
<el-select v-model="selected" multiple @click.native="handleClick">
<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
</el-select>
```
```js
methods: {
handleClick() {
const options = document.querySelectorAll('.el-select-dropdown__item')
}
}
```
2. 遍历选项元素,给每个元素绑定 @click 事件,并在事件处理函数中获取点击的单个元素:
```js
methods: {
handleClick() {
const options = document.querySelectorAll('.el-select-dropdown__item')
options.forEach(option => {
option.addEventListener('click', () => {
console.log(option) // 获取点击的单个元素
})
})
}
}
```