简体   繁体   English

为什么不能删除同级事件监听器?

[英]Why can't remove the event listener on sibling's?

 function show1() { console.log("ok1"); document.getElementById("a2").removeEventListener("click", delegate); } function show2() { console.log("ok2"); } function show3() { console.log("ok3"); } function delegate(event) { var flag = event.target; switch (flag.id) { case "a1": show1(); break; case "a2": show2(); break; case "a3": show3(); break; } } ob = document.getElementById("tl"); ob.addEventListener("click", delegate); 
 <ul id="tl"> <li id="a1">a1</li> <li id="a2">a2</li> <li id="a3">a3</li> </ul> 

The parent node ul contains three son nodes li ,bind a event listener on the parent node ,let ul delegate all node's event listening. 父节点ul包含三个子节点li ,在父节点上绑定一个事件侦听器,让ul委托所有节点的事件侦听。
My expectation: 我的期望:
when you click a1 , show1 function will remove event listener on its sibling whose id is a2 . 当您单击a1show1函数将删除ID为a2同级事件监听器。
That is to say,you click li whose id is a2 after clicking li whose id is a1 , ok2 will not show on console. 也就是说,你点击li id为a2点击后li id为a1ok2不会显示在控制台上。

The real action: 实际动作:
You click li whose id is a2 after clicking li whose id is a1 , ok2 will show on console. 您单击li id为a2点击后li id为a1ok2将显示在控制台上。

Why 为什么

document.getElementById("a2").removeEventListener("click",delegate);

can't remove the event listener on sibling's? 无法删除同级事件监听器?
How to fix it? 如何解决?

If a listener is attached to an element, the only thing you can do about removing it is to remove the same listener from the same element - you can't remove it from a child element because it's not attached to the child element. 如果将侦听器附加到元素,则删除它的唯一操作就是从同一元素中删除相同的侦听器-您不能从子元素中删除它,因为它没有附加到子元素。

For the functionality you're looking for, you might add the node you want to "remove" the click functionality from to a Set , and in the parent node's handler, check that the event.target (the flag variable) is not included in that Set : 对于您要寻找的功能,可以将要从中“删除”点击功能的节点添加到Set ,然后在父节点的处理程序中,检查event.targetflag变量)是否未包含在其中。该Set

 const elementsToIgnore = new Set(); function show1() { console.log("ok1"); elementsToIgnore.add(document.getElementById("a2")); } function show2() { console.log("ok2"); } function show3() { console.log("ok3"); } function delegate(event) { var flag = event.target; if (elementsToIgnore.has(flag)) return; switch (flag.id) { case "a1": show1(); break; case "a2": show2(); break; case "a3": show3(); break; } } ob = document.getElementById("tl"); ob.addEventListener("click", delegate); 
 <ul id="tl"> <li id="a1">a1</li> <li id="a2">a2</li> <li id="a3">a3</li> </ul> 

This is beacus you are adding event listener to ul not li . 这是您要将事件侦听器添加到ul not li Add event handler to li as following event处理程序添加到li如下

 function show1(){ console.log("ok1"); document.getElementById("a2").removeEventListener("click",delegate); } function show2(){ console.log("ok2"); } function show3(){ console.log("ok3"); } function delegate(event) { var flag = event.target; switch(flag.id){ case "a1": show1(); break; case "a2": show2(); break; case "a3": show3(); break; } } ob = document.getElementsByTagName("li"); for(let i=0; i<ob.length; i++){ ob[i].addEventListener("click",delegate); } 
 <ul id="tl"> <li id="a1">a1</li> <li id="a2">a2</li> <li id="a3">a3</li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM