简体   繁体   中英

Javascript : RemoveEventListener with Anonym Function

How to remove and event listener with an Anonym function , with removeEventListener() ;

document.getElementById("object").onclick = function(e){
    if(e && e.stopPropagation) {
        e.stopPropagation();
    } else {
          e = window.event;
          e.cancelBubble = true;
    }
}

So I have this piece of code and the function what's called must be anonym I'dont know why but If it's not then doesn't works correctly, maybe beacuse of the event :|

But If it's anonym how can I remove it?

Well you havent added an actual event listener, you have just populated the onclick variable with a function to be run. So you should be able to just use something like this:

document.getElementById("object").onclick = false;

EDIT

Just tried it in jsFiddle and what I suggested works.

只需为其提供一个空值,这是未初始化onclick时的起始值: document.getElementById("object").onclick = null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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