简体   繁体   English

document.body.removeEventListener删除所有单击事件侦听器(不是指定的函数),为什么?

[英]document.body.removeEventListener removes ALL click event listeners (not the specified function) why?

document.body.removeEventListener("click", delElm);

removes all "onclick" events for the entire page. 删除整个页面的所有“ onclick”事件。 why? 为什么? Am I not clearly specifying what function I want removed from the document.body? 我是否没有明确指定要从document.body中删除的功能?

document.body.innerHTML += "<div id='dynamicLinkCheckOutput'></div>";
var dynamicLinkCheckOutput = document.getElementById('dynamicLinkCheckOutput');

function delElm(e){
    var dynamicLinkCheckOutput = document.getElementById('dynamicLinkCheckOutput');        
       dynamicLinkCheckOutput.parentNode.removeChild(dynamicLinkCheckOutput);
    //document.body.removeEventListener("click", delElm); //moved for clairity
}
setTimeout(function(){
    dynamicLinkCheckOutput.onclick = function(e){
        e.stopPropagation();
    };
    document.body.addEventListener("click", delElm);
    document.body.removeEventListener("click", delElm); //ALL click events from ALL elements have been removed the moment this line executes. 
}, 0);
 document.body.removeEventListener("click", delElm); 

removes all "onclick" events for the entire page 删除整个页面的所有“ onclick”事件

No, it doesn't. 不,不是。 But this does: 但这确实是:

document.body.innerHTML += "<div id='dynamicLinkCheckOutput'></div>";

Using += on innerHTML forces the browser to: innerHTML上使用+=强制浏览器执行以下操作:

  • Spin through all of the elements inside body building an HTML string for them. 遍历body所有元素,为它们构建HTML字符串。
  • Return that string to the JavaScript layer, which then adds on the string on the right-hand side. 将该字符串返回到JavaScript层,然后在右侧添加该字符串。
  • Parse the string assigned back to innerHTML by the JavaScript layer, wipe out all elements within body (thus losing their event handlers and most other state), and create new , replacement elements for them from the parsed HTML. 解析由JavaScript层分配回innerHTML的字符串,擦除body所有元素(从而失去其事件处理程序和大多数其他状态),并从解析的HTML中为其创建新的替换元素。

If you want to append to body (or any other element), don't use innerHTML += x , use: 如果要附加到body (或任何其他元素),请不要使用innerHTML += x ,请使用:

暂无
暂无

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

相关问题 document.body.removeEventListener 不适用于删除按键事件的侦听器 - document.body.removeEventListener not working for removing listener for keypress event document.open 删除所有窗口侦听器 - document.open removes all window listeners Chrome会删除事件监听器吗? - Chrome removes event listeners? 单击附加到 document.body 的事件侦听器不会在 iOS6 中触发? - click event listeners attached to document.body dont fire in iOS6? 为什么在 Javascript 中添加新的单击事件侦听器会删除所有对象的所有先前单击事件侦听器? - Why Does Adding a New Click EventListener in Javascript Remove all Previous Click Event Listeners for All Objects? 向文档中的所有帧添加事件侦听器 - To add event listeners to all the frames in a document 将大量元素事件侦听器委托给 document.body 是不是很疯狂? - Is it crazy to delegate tons of element event listeners to document.body? jQuery文档单击取消绑定,删除所有子级Click事件 - JQuery document click unbind Removes All Children Click Events 注册多个事件侦听器,即使我正在使用removeEventListener - Registering multiple event listeners even though I'm using removeEventListener 当我以ID替换DOCUMENT或BODY的ID为目标的事件时,在jQuery中使用on()函数的Click事件不起作用 - Click event using the on() function in jQuery is not working when I target an event with an ID in replace of DOCUMENT or BODY
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM