Do I need to unbind jquery event before remove element?

前端 未结 3 798
北荒
北荒 2020-12-24 10:37

I have a page using jquery-ui-dialog. Each time the dialog opens, page contents load in using ajax. Then it binds some event using jquery \"on()\". When the dialog close,

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-24 11:16

    Hey I know this is an old question but I believe the accepted answer is misleading.

    Although it's correct to say that you will need to unbind events on raw JS to avoid memory leaks on old browsers (ehem IE), calling remove() or empty() will already do that for you.

    So your current call to empty() should be enough, it doesn't need to be preceded by unbind()

    From jQuery docs (http://api.jquery.com/empty/)

    To avoid memory leaks, jQuery removes other constructs such as data and event handlers from the child elements before removing the elements themselves.

提交回复
热议问题