简体   繁体   中英

How can I event to antoher function from addEventListener without anonymous function

I want to use 'addEventListener' to get something from 'event', but after used I have to use 'removeEventListener' to remove event listener. How can I do for this situation?

example:

    var elem = document.createElement('div');

    // I need argument of *event*
    elem.addEventListener('click', function(*event*){
         console.log(*event*)
    });

    // after used, I have to remove it, but only named function can be removed
    elem.removeEventListener('click', function(event){

    });
    // this way can be actually removed, but I need *event*
    elem.removeEventListener('click', namedFunc);

How can I resolve this issue??

I'm curious about the way you did it, btw

function once(el, ev, cb){
    el.addEventListener(ev, function f(event){
        cb.call(event, event);
        el.removeEventListener(ev, f);
    }, false);
}

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