[英]Click event target gives element or it's child, not parent element
我有這個 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
元素,或者它的a
元素( h4
或p
)。
即使用戶單擊其中一個子元素,我如何獲取a
元素(帶有@click
處理程序的元素)?
使用event.currentTarget
指向您附加偵聽器的元素。 它不會隨着事件冒泡而改變
https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget
使用 CSS 的替代解決方案:
a * {
pointer-events: none;
}
元素永遠不是指針事件的目標; 但是,如果這些后代將指針事件設置為其他值,則指針事件可能會針對其后代元素。 在這些情況下,指針事件將在事件捕獲/冒泡階段期間,在它們往返后代的途中適當地觸發此父元素上的事件偵聽器。
參考: https : //developer.mozilla.org/en-US/docs/Web/CSS/pointer-events#Values
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.