vue?事件獲取當(dāng)前組件的屬性方式
vue事件獲取當(dāng)前組件屬性
for循環(huán)把當(dāng)前的item傳遞給綁定事件:
<block v-for="(item, index) in data_list">
<div v-on:click='changeType(item)' class="ci ">
<div class="cib" formType="submit" style='padding:15rpx 0 0 0;'>
<div style='color: #1baf1b;' id="item">{{item.productName}}</div>
<div style='color: #1baf1b;' id="item">售價(jià):{{item.productPrice/100}}元</div>
</div>
</div>
</block> changeType: function(event) {
console.log(event);
console.log(event.productId);
}打印結(jié)果。//取到的數(shù)據(jù)是數(shù)組循環(huán)渲染出來當(dāng)前點(diǎn)擊的下標(biāo)對(duì)象

click事件獲取當(dāng)前元素屬性
Vue可以傳遞$event對(duì)象
<body id="app">
? <ul>
? ? <li @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>currentTarget:currentTarget 事件屬性返回其監(jiān)聽器觸發(fā)事件的節(jié)點(diǎn),即當(dāng)前處理該事件的元素、文檔或窗口。
通俗一點(diǎn)說,就是你的點(diǎn)擊事件綁定在哪一個(gè)元素上,currentTarget獲取到的就是哪一個(gè)元素。
Event對(duì)象的一些兼容性寫法
//獲得event對(duì)象兼容性寫法 event || (event = window.event); //獲得target兼容型寫法 event.target||event.srcElement //阻止瀏覽器默認(rèn)行為兼容性寫法 event.preventDefault ? event.preventDefault() : (event.returnValue = false); //阻止冒泡寫法 event.stopPropagation ? event.stopPropagation() : (event.cancelBubble = true);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue子組件設(shè)計(jì)provide和inject理解使用
這篇文章主要為大家介紹了vue子組件設(shè)計(jì)provide和inject理解及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Vue中的父子組件通訊以及使用sync同步父子組件數(shù)據(jù)
這篇文章主要介紹了Vue中的父子組件通訊以及使用sync同步父子組件數(shù)據(jù),對(duì)vue感興趣的同學(xué),可以參考下2021-04-04
使用Vite2+Vue3渲染Markdown文檔的方法實(shí)踐
本文主要介紹了Vite2+Vue3渲染Markdown文檔的方法實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下2021-08-08
vue-element-admin登錄攔截設(shè)置白名單方式
這篇文章主要介紹了vue-element-admin登錄攔截設(shè)置白名單方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
在vue項(xiàng)目中,將juery設(shè)置為全局變量的方法
今天小編就為大家分享一篇在vue項(xiàng)目中,將juery設(shè)置為全局變量的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09

