Cancel anonymous function

Can someone tell me how to "untie" an anonymous function? JQUERY, he is able to do this, but how can I implement this functionality in my own script.

This is the scenario:

The following code attaches the "onclick" event to a Div that has "someDivId" as an identifier, now when you click on the DIV, it shows "clicked!".

var a  = document.getElementById('someDivId');
bindEvent(a,'click',function(){alert('clicked!');});

That everything is in order, the problem is how to "detach" the function from the DIV if the function is anonymous or how to "not attach" all attached events to the "a" element?

unBind(a,'click'); //Not necessarily the given params, it just an example.

This is the code for the bindEvent method:

function bindEvent (el,evtType,fn){
    if ( el.attachEvent ) {
        el['e'+evtType+fn] = fn;
        el[evtType+fn] = function(){
            fn.call(el,window.event);
        }
        el.attachEvent( 'on'+evtType, el[evtType+fn] );
    } else {
        el.addEventListener( evtType, fn, false );
    }
}
+5
source share
5 answers

, , , , ... ! ( IE9, Firefox 12, Chrome 18)

addEvent() removeEvent(). ( JQuery!)

HELPERS.removeEvent = document.removeEventListener ?
function( type, handle,el ) {
    if ( el.removeEventListener ) {
    //W3C Standard    
    el.removeEventListener( type, handle, true );
    }
} : 
function( type, handle,el ) {
    if ( el.detachEvent ) {
        //The IE way
        el.detachEvent( 'on'+type, el[type+handle] );
        el[type+handle] = null;
    }
};

HELPERS.addEvent = document.addEventListener ?
function( type, handle,el ) {
    if ( el.addEventListener ) {
        //W3C Standard
        el.addEventListener( type, handle, true );
    }
} : 
function( type, handle,el ) {
    if ( el.attachEvent ) {
        //The IE way
        el['e'+type+handle] = handle;
        el[type+handle] = function(){
            handle.call(el,window.event);
        };
        el.attachEvent( 'on'+type, el[type+handle] );

    }
}

- "" , :

HELPERS.EVTS = {};

, , . : , () () (el).

    function bindEvent(event, handler,el) {

            if(!(el in HELPERS.EVT)) {
                // HELPERS.EVT stores references to nodes
                HELPERS.EVT[el] = {};
            }

            if(!(event in HELPERS.EVT[el])) {
                // each entry contains another entry for each event type
                HELPERS.EVT[el][event] = [];
            }
            // capture reference
            HELPERS.EVT[el][event].push([handler, true]);
            //Finally call the aux. Method
            HELPERS.addEvent(event,handler,el);

         return;

    }

, , () (el)

    function removeAllEvent(event,el) {

            if(el in HELPERS.EVT) {
                var handlers = HELPERS.EVT[el];
                if(event in handlers) {
                    var eventHandlers = handlers[event];
                    for(var i = eventHandlers.length; i--;) {
                        var handler = eventHandlers[i];
                        HELPERS.removeEvent(event,handler[0],el);

                    }
                }
            }   

    return;

    }

, , : DOM Node

    var a = document.getElementById('some_id');

'bindEvent()' .

    bindEvent('click',function(){alert('say hi');},a);

:

    removeAllEvent('click',a);

, , - -.

+4

( , "" , , , ), on* , .

.

var a = document.getElementById('someDivId');
a.onclick = function() {alert("Clicked!");};
// later...
a.onclick = null;

, , . , .

+2

, , javascript. , DOM . - .

+1

JavaScript , node.

node, Node.cloneNode, . : https://developer.mozilla.org/En/DOM/Node.cloneNode

node (), , .

:

function noop() {}
bindEvent(myElement, "click", noop);
+1

jquery :

jQuery.removeEvent = document.removeEventListener ?
    function( elem, type, handle ) {
        if ( elem.removeEventListener ) {
            elem.removeEventListener( type, handle, false );
        }
    } :
    function( elem, type, handle ) {
        if ( elem.detachEvent ) {
            elem.detachEvent( "on" + type, handle );
        }
    };
-1

All Articles