我有这个HTML元素:
而这个Javascript:
问题在于
如果用户点击其中一个子元素,我如何获取
<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" v-text="notification.title" />
<p class="list-group-item-text" v-text="notification.created_at|moment" />
</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
处理程序)?
this
传递给你的函数showDetails
吗?像这样showDetails(notification, $event, this)
,你的第三个参数应该是你的锚点。 - Kaddaththis
代替event.target
吗? - D. Pardalthis
将返回 Window 实例本身。 - Stephan Vierkant