【问题标题】:Get the ID of the Current div in VueJS获取 VueJS 中当前 div 的 ID
【发布时间】:2021-05-01 21:51:23
【问题描述】:
我正在为我的div 到v-for 生成一个动态id,我必须将此id 传递给某个函数。
<div
v-for="(item, index) in items"
:key="index"
:id="'form' + index"
>
<button onclick="function(<id of the div>)"></button>
</div>
有什么办法可以做到吗?
【问题讨论】:
标签:
javascript
html
vue.js
vuejs2
vue-component
【解决方案1】:
将 id 传递给函数,就像在绑定中使用它一样:
<button @click="function('form' + index)"></button>
v-for 创建可用于重复元素及其子元素的块范围变量。
同时使用@click 指令代替onclick。例如,假设您有一个名为“handler”的方法,您的组件可能如下所示:
<button @click="handler('form' + index)"></button>
methods: {
handler(id) {
console.log(id);
}
}