vue.js click點(diǎn)擊事件獲取當(dāng)前元素對(duì)象的操作
Vue.js可以傳遞$event對(duì)象
<body id="app">
<ul>
<li v-on:click="say('hello!', $event)">點(diǎn)擊當(dāng)前行文本</li>
<li>li2</li>
<li>li3</li>
</ul>
<script>
new Vue({
el: '#app',
data: {
message: 'Hello Vue.js!'
},
methods: {
say: function(msg, event) {
//獲取點(diǎn)擊對(duì)象
var el = event.currentTarget;
alert("當(dāng)前對(duì)象的內(nèi)容:"+el.innerHTML);
}
}
})
</script>
</body>
| 屬性 | 描述 |
|---|---|
| bubbles | 返回布爾值,指示事件是否是起泡事件類型。 |
| cancelable | 返回布爾值,指示事件是否可擁可取消的默認(rèn)動(dòng)作。 |
| currentTarget | 返回其事件監(jiān)聽器觸發(fā)該事件的元素。 |
| eventPhase | 返回事件傳播的當(dāng)前階段。 |
| target | 返回觸發(fā)此事件的元素(事件的目標(biāo)節(jié)點(diǎn))。 |
| timeStamp | 返回事件生成的日期和時(shí)間。 |
| type | 返回當(dāng)前 Event 對(duì)象表示的事件的名稱。 |
currentTarget:currentTarget 事件屬性返回其監(jiān)聽器觸發(fā)事件的節(jié)點(diǎn),即當(dāng)前處理該事件的元素、文檔或窗口。
通俗一點(diǎn)說,就是你的點(diǎn)擊事件綁定在哪一個(gè)元素上,currentTarget獲取到的就是哪一個(gè)元素。
target:target 事件屬性可返回事件的目標(biāo)節(jié)點(diǎn)(觸發(fā)該事件的節(jié)點(diǎn)),如生成事件的元素、文檔或窗口。
通俗一點(diǎn)說,就是你當(dāng)前點(diǎn)擊的是哪一個(gè)元素,target獲取到的就是哪一個(gè)元素。
<li v-for="img in willLoadImg" @click="selectImg($event)">
<img class="loadimg" :src="img.url" :data-id="img.id" alt="">
</li>
methods: {
selectImg(event) {
console.log(event.currentTarget);
console.log(event.target);
}
}
補(bǔ)充知識(shí):vue獲取當(dāng)前點(diǎn)擊對(duì)象的下標(biāo),和當(dāng)前點(diǎn)擊對(duì)象的內(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;
//獲取點(diǎn)擊對(duì)象
var el = event.currentTarget;
console.log("當(dāng)前對(duì)象的內(nèi)容:"+el.innerHTML);
console.log(this.current)
}
以上這篇vue.js click點(diǎn)擊事件獲取當(dāng)前元素對(duì)象的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+springboot用戶注銷功能實(shí)現(xiàn)代碼
這篇文章主要介紹了vue+springboot用戶注銷功能,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05
vite+vue3.0+ts+element-plus快速搭建項(xiàng)目的實(shí)現(xiàn)
本文將結(jié)合實(shí)例代碼,介紹vite+vue3.0+ts+element-plus快速搭建項(xiàng)目的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
Element中select多數(shù)據(jù)加載優(yōu)化的實(shí)現(xiàn)
本文主要介紹了Element中select多數(shù)據(jù)加載優(yōu)化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vue點(diǎn)擊input彈出帶搜索鍵盤并監(jiān)聽該元素的方法
今天小編就為大家分享一篇vue點(diǎn)擊input彈出帶搜索鍵盤并監(jiān)聽該元素的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08

