vue v-for 點擊當(dāng)前行,獲取當(dāng)前行數(shù)據(jù)及event當(dāng)前事件對象的操作
前言
在 v-for 循環(huán)語句上,定義一個點擊事件 傳入兩個參數(shù)(當(dāng)行數(shù)據(jù)、當(dāng)前事件對象),如下代碼片段,當(dāng)前事件對象必須加上 ‘$' 符號
<template>
<div>
<ul>
<li
v-for="(item, index) in arrData"
:key="index"
@click="operate(item, $event)"
>
{{ item.title }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
arrData: [
{ id: 1, title: '第一條數(shù)據(jù)' },
{ id: 2, title: '第二條數(shù)據(jù)' }
]
};
},
methods: {
operate(item, event) {
console.log(item);
console.log(event);
}
}
};
</script>
不加'$‘報錯:

加上'$‘: 點擊行之后獲得當(dāng)前行數(shù)據(jù) 以及當(dāng)前事件對象

如果本篇文章對你有幫助的話,很高興能夠幫助上你。
補充知識:vue獲取當(dāng)前點擊對象的下標,和當(dāng)前點擊對象的內(nèi)容
如下所示:
<li v-for="(item,index) in tabList" v-on:click="addClass(index,$event)" >{{item.title}}</li>
data里面聲明:
data() {
return {
tabList: [
{ id: 0, title: "首頁1" },
{ id: 1, title: "首頁2" },
{ id: 2, title: "首頁3" }
],
current:0
};
},
methods: {
addClass: function(index,event) {
this.current = index;
//獲取點擊對象
var el = event.currentTarget;
console.log("當(dāng)前對象的內(nèi)容:"+el.innerHTML);
console.log(this.current)
}
以上這篇vue v-for 點擊當(dāng)前行,獲取當(dāng)前行數(shù)據(jù)及event當(dāng)前事件對象的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用elementUI分頁如何實現(xiàn)切換頁面時返回頁面頂部
這篇文章主要介紹了vue使用elementUI分頁如何實現(xiàn)切換頁面時返回頁面頂部,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
關(guān)于vue.js中this.$emit的理解使用
本文主要介紹了關(guān)于vue.js中this.$emit的理解使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
如何使用vue實現(xiàn)前端導(dǎo)入excel數(shù)據(jù)
在實際開發(fā)中導(dǎo)入功能是非常常見的,導(dǎo)入功能前端并不難,下面這篇文章主要給大家介紹了關(guān)于如何使用vue實現(xiàn)前端導(dǎo)入excel數(shù)據(jù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-04-04

