javascript - 单击事件目标给出元素或其子元素,而不是父元素
问题描述
我有这个 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/en-US/docs/Web/API/Event/currentTarget
推荐阅读
- java - 有人可以向我解释这个例子中的 PriorityQueue 吗?
- reactjs - materialize-css 和 Material UI 的区别
- android - 具有相同包名的两个不同项目
- android - Android 内容提供程序查询未检索到更新的信息
- sql-server - SSAS 处理错误:指定的查询太复杂,无法作为单个语句进行评估
- spring - Spring Batch 无法配置 2 个读卡器和拆分作业
- javascript - 轮播滑块创建自定义当用户鼠标悬停到轮播滑块时如何停止
- c# - 如何在 WPF 中获取当前的模态窗口
- excel - 在 Delphi 2009 中获取没有 $ 的 Excel 单元格地址
- reactjs - 当我滚动 Flatlist 时,内存使用率越来越高,当我停止滚动时内存没有释放(React Native)