点击事件目标提供元素或其子元素,而不是父元素

史蒂芬广场:

我有这个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元素,也可以是其子元素(h4p)。

即使用户单击子a元素之一,如何获取该元素(带有@click处理程序的元素)?

易卜拉欣单宁:

使用event.currentTargetwhich指向您附加了侦听器的元素。它不会随着事件起泡而改变

https://developer.mozilla.org/zh-CN/docs/Web/API/Event/currentTarget

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章