我有这个HTML元素:
<div class="list-group">
<a href="javascript:;" @click="showDetails(notification, $event)" class="list-group-item" v-for="notification in notifications" :key="notification.id">
<h4 class="list-group-item-heading">{{ '{{ notification.title }}' }}</h4>
<p class="list-group-item-text">{{ '{{ notification.created_at|moment }}' }}</p>
</a>
</div>
而这个JavaScript:
return new Vue({
methods: {
showDetails: function (notification, event) {
this.notification = notification
console.info(event.target)
}
}
}
问题是event.target
返回我单击的确切元素。这意味着它可以是a
元素,也可以是其子元素(h4
或p
)。
即使用户单击子a
元素之一,如何获取该元素(带有@click
处理程序的元素)?
使用event.currentTarget
which指向您附加了侦听器的元素。它不会随着事件起泡而改变
https://developer.mozilla.org/zh-CN/docs/Web/API/Event/currentTarget
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句