jQuery : Are events handlers removed from objects if they are removed from the DOM using html()

后端 未结 2 1358
南旧
南旧 2021-02-15 10:47

I am concerned about memory leaks in my application as I use jquery\'s html() method a lot to replace content in the the DOM. I just want to make sure that not of these

2条回答
  •  -上瘾入骨i
    2021-02-15 11:16

    It seems using bind in jQuery, you can have more control over the events and handlers such as in this example from http://api.jquery.com/unbind/

    var myHandlers = {};    
    
    myHandlers.handler = function() {
        alert('The quick brown fox jumps over the lazy dog.');
    };
    $('#foo').bind('click', handler);
    $('#foo').unbind('click', handler);
    
    delete(myHandlers.handler);
    

    But I don't know if that is possible to control with the normal jQuery syntax of $('a').click() since all that is returned is a jQuery object and no references to the handlers or events.

    There is related discussion to this question here on stack overflow:

    javascript memory leaks

提交回复
热议问题