且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Javascript / DOM:如何清除DOM对象的所有事件?

更新时间:2023-12-05 14:52:34

我不知道你是什么意味着使用删除所有事件。删除特定类型事件的所有处理程序或所有事件处理程序的一种类型?



删除所有事件处理程序



如果要删除所有事件处理程序(任何类型),可以克隆 元素并将其替换为克隆:

  var clone = element.cloneNode(); 
while(element.firstChild){
clone.appendChild(element.lastChild);
}
element.parentNode.replaceChild(clone,element);

注意:这将保留属性和子项,但不会保留对DOM属性的任何更改。






删除特定类型的匿名事件处理程序



另一种方法是使用 removeEventListener() ,但我猜你已经尝试过了,没有办法。 这是抓到


将匿名函数调用 addEventListener 每次创建一个新的侦听器。调用 removeEventListener 到匿名函数不起作用。匿名函数每次调用时创建唯一的对象,它不是对现有对象的引用,尽管它可以调用它。当以这种方式添加事件监听器时,确保只添加一次,它是永久性的(不能删除),直到被添加到的对象被销毁。


您实际上是将 addEventListener 的匿名函数传递给 eventReturner 返回一个函数。 / p>

您必须可以解决这个问题:


  1. 不要使用返回函数的函数。直接使用函数:

      function handler(){
    dosomething();
    }

    div.addEventListener('click',handler,false);


  2. addEventListener创建一个包装器存储对返回函数的引用,并创建一些奇怪的 removeAllEvents 函数:

      var _eventHandlers = {}; // someplace global 

    function addListener(node,event,handler,capture){
    if(!(_eventHandlers中的节点)){
    // _eventHandlers存储对节点的引用
    _eventHandlers [node] = {};
    }
    如果(!(_eventHandlers [node]中的事件)){
    //每个条目包含每个事件类型的另一个条目
    _eventHandlers [node] [event] = [ ]。
    }
    //捕获引用
    _eventHandlers [node] [event] .push([handler,capture]);
    node.addEventListener(event,handler,capture);
    }

    function removeAllListeners(node,event){
    if(node in _eventHandlers){
    var handlers = _eventHandlers [node];
    if(处理程序中的事件){
    var eventHandlers = handlers [event];
    for(var i = eventHandlers.length; i--;){
    var handler = eventHandlers [i];
    node.removeEventListener(event,handler [0],handler [1]);
    }
    }
    }
    }

    那么你可以使用它:

      addListener(div,'click',eventReturner(),false)
    / /和以后
    removeListeners(div,'click')


演示



注意:如果您的代码运行了很长时间,并且您正在创建和删除大量元素,则必须确保删除 _eventHandlers ,当您销毁它们。


Just question: Is there any way to completely remove all events of an object, e.g. a div?

EDIT: I'm adding per div.addEventListener('click',eventReturner(),false); an event.

function eventReturner() {
    return function() {
        dosomething();
    };
}

EDIT2: I found a way, which is working, but not possible to use for my case:

var returnedFunction;
function addit() {
    var div = document.getElementById('div');
    returnedFunction = eventReturner();
    div.addEventListener('click',returnedFunction,false); //You HAVE to take here a var and not the direct call to eventReturner(), because the function address must be the same, and it would change, if the function was called again.
}
function removeit() {
    var div = document.getElementById('div');
    div.removeEventListener('click',returnedFunction,false);
}

I am not sure what you mean with remove all events. Remove all handlers for a specific type of event or all event handlers for one type?

Remove all event handlers

If you want to remove all event handlers (of any type), you could clone the element and replace it with its clone:

var clone = element.cloneNode();
while (element.firstChild) {
  clone.appendChild(element.lastChild);
}
element.parentNode.replaceChild(clone, element);

Note: This will preserve attributes and children, but it will not preserve any changes to DOM properties.


Remove "anonymous" event handlers of specific type

The other way is to use removeEventListener() but I guess you already tried this and it didn't work. Here is the catch:

Calling addEventListener to an anonymous function creates a new listener each time. Calling removeEventListener to an anonymous function has no effect. An anonymous function creates a unique object each time is is called, it is not a reference to an existing object though it may call one. When adding an event listener in this manner be sure it is added only once, it is permanent (can not be removed) untill the object it was added to is destroyed.

You are essentially passing an anonymous function to addEventListener as eventReturner returns a function.

You have to possibilites to solve this:

  1. Don't use a function that returns a function. Use the function directly:

    function handler() {
        dosomething();
    }
    
    div.addEventListener('click',handler,false);
    

  2. Create a wrapper for addEventListener that stores a reference to the returned function and create some weird removeAllEvents function:

    var _eventHandlers = {}; // somewhere global
    
    function addListener(node, event, handler, capture) {
        if(!(node in _eventHandlers)) {
            // _eventHandlers stores references to nodes
            _eventHandlers[node] = {};
        }
        if(!(event in _eventHandlers[node])) {
            // each entry contains another entry for each event type
            _eventHandlers[node][event] = [];
        }
        // capture reference
        _eventHandlers[node][event].push([handler, capture]);
        node.addEventListener(event, handler, capture);
     }
    
    function removeAllListeners(node, event) {
        if(node in _eventHandlers) {
            var handlers = _eventHandlers[node];
            if(event in handlers) {
                var eventHandlers = handlers[event];
                for(var i = eventHandlers.length; i--;) {
                    var handler = eventHandlers[i];
                    node.removeEventListener(event, handler[0], handler[1]);
                }
            }
        }
    }
    

    And then you could use it with:

    addListener(div, 'click', eventReturner(), false)
    // and later
    removeListeners(div, 'click')
    

DEMO

Note: If your code runs for a long time and you are creating and removing a lot of elements, you would have to make sure to remove the elements contained in _eventHandlers when you destroy them.