Do events handlers on a DOM node get deleted with the node?

后端 未结 5 678
死守一世寂寞
死守一世寂寞 2020-12-01 06:22

(Note: I\'m using jQuery below, but the question is really a general Javascript one.)

Say I\'ve got a div#formsection whose contents are repeatedly upda

5条回答
  •  攒了一身酷
    2020-12-01 06:56

    I wanted to know myself so after a little test, I think the answer is yes.

    removeEvent is called when you .remove() something from the DOM.

    If you want see it yourself you can try this and follow the code by setting a breakpoint. (I was using jquery 1.8.1)

    Add a new div first:
    $('body').append('

    ')

    Check $.cache to make sure there is no events attached to it. (it should be the last object)

    Attach a click event to it:
    $('#test').on('click',function(e) {console.log("clicked")});

    Test it and see a new object in $.cache:
    $('#test').click()

    Remove it and you can see the object in $.cache is gone as well:
    $('#test').remove()

提交回复
热议问题