清除dom节点子节点时自动删除事件处理程序

时间:2010-11-02 19:21:13

标签: javascript event-handling introspection nodes removechild

有没有办法内省dom节点以查看是否附加了事件处理程序,因此您可以有效地编写一个安全函数来清除dom节点而不必担心事件处理程序留下的内存泄漏?我希望以通用的方式做到这一点。

2 个答案:

答案 0 :(得分:0)

从我看到的可能无法实现,这里引用了mozilla网站:

https://developer.mozilla.org/en/DOM/element.addEventListener#Memory_issues

  

内存问题

     

document.addEventListener( “负载”,   function(event){obj.func(event); },   假);

     

将addEventListener调用到   匿名函数创建一个新的   每次听众。调用   removeEventListener为匿名   功能无效。一个匿名的   函数创建一个唯一的对象   时间被称为,它不是一个   但是,引用现有对象   它可以叫一个。添加活动时   听众以这种方式确定它是   只添加一次,它是永久性的(可以   不被删除)直到对象它   被添加到被销毁。

如果听众不是匿名的,你可以这样做。这是来自YUI库事件的一段代码:

        /**
         * Returns all listeners attached to the given element via addListener.
         * Optionally, you can specify a specific type of event to return.
         * @method getListeners
         * @param el {HTMLElement|string} the element or element id to inspect 
         * @param sType {string} optional type of listener to return. If
         * left out, all listeners will be returned
         * @return {Object} the listener. Contains the following fields:
         *   type:   (string)   the type of event
         *   fn:     (function) the callback supplied to addListener
         *   obj:    (object)   the custom object supplied to addListener
         *   adjust: (boolean|object)  whether or not to adjust the default context
         *   scope: (boolean)  the derived context based on the adjust parameter
         *   index:  (int)      its position in the Event util listener cache
         * @static
         */           
        getListeners: function(el, sType) {
            var results=[], searchLists;
            if (!sType) {
                searchLists = [listeners, unloadListeners];
            } else if (sType === "unload") {
                searchLists = [unloadListeners];
            } else {
                sType = this._getType(sType);
                searchLists = [listeners];
            }

            var oEl = (YAHOO.lang.isString(el)) ? this.getEl(el) : el;

            for (var j=0;j<searchLists.length; j=j+1) {
                var searchList = searchLists[j];
                if (searchList) {
                    for (var i=0,len=searchList.length; i<len ; ++i) {
                        var l = searchList[i];
                        if ( l  && l[this.EL] === oEl && 
                                (!sType || sType === l[this.TYPE]) ) {
                            results.push({
                                type:   l[this.TYPE],
                                fn:     l[this.FN],
                                obj:    l[this.OBJ],
                                adjust: l[this.OVERRIDE],
                                scope:  l[this.ADJ_SCOPE],
                                index:  i
                            });
                        }
                    }
                }
            }

            return (results.length) ? results : null;
        },

你可以在这里阅读更多内容: http://developer.yahoo.com/yui/event/

答案 1 :(得分:0)

这取决于。可以有效删除由el.onclick = ...等属性分配的简单事件处理程序,但没有通过 IE 中的attachEvent()添加的处理程序列表。 Memory leaks在其他浏览器中并不是很受关注。

/**
* The purge function takes a reference to a DOM element as an argument. 
* It loops through the element's attributes. If it finds any functions, 
* it nulls them out. This breaks the cycle, allowing memory to be reclaimed. 
* It will also look at all of the element's descendent elements, and clear 
* out all of their cycles as well.
*  - http://javascript.crockford.com/memory/leak.html
*/
function purge(d) {
    var a = d.attributes, i, l, n;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            n = a[i].name;
            if (typeof d[n] === 'function') {
                d[n] = null;
            }
        }
    }
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            purge(d.childNodes[i]);
        }
    }
}

如果您要处理这两种情况并管理页面中的所有事件处理程序,则可以在您自己的函数中包装addEvent 功能,并在要删除元素时将其删除