[英]How can I detect whether bubbling encounters a certain type of element?
我有幾個div元素,每個元素都有一個畫布:
我決定為ID為all的div添加一個偵聽器。 如何檢測該div上的點擊是否也與class dog一起在div上點擊?
我試過了
document.getElementById('everything').addEventListener('click', function(e) {
console.debug(e.target);
});
但是console.debug(e.target);
只給我被點擊的畫布。
我可以簡單地為dog div分配一個事件偵聽器,但是我不想這樣做,因為在div中ID為“ everything”的孩子div可能很多。
抱歉,這是jQuery,但是請嘗試此操作。
而這幾乎是不可能的,以確保如果#everything
是父母.dog
或者雖然不
情況“ #everything”是“ .dog”的父級,這應該可以工作。
$("#everything").on("click",function(e){
if $(this).closest(".dog").length > 0
console.log("element covering both #everything and .dog has clicked")
else
console.log("#everything has clicked but not .dog")
})
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.