Are events lost in jQuery when you remove() an element and append() it elsewhere?

后端 未结 3 1488
隐瞒了意图╮
隐瞒了意图╮ 2021-02-18 14:35

What happens in jQuery when you remove() an element and append() it elsewhere?

It appears that the events are unhooked - as if you were just inserting fresh html (which

3条回答
  •  醉酒成梦
    2021-02-18 15:23

    The jQuery detach() function is the same as remove() but preserves the event handlers in the object that it returns. If you have to remove the item and place it somewhere else with everything you can just use this.

    var objectWithEvents = $('#old').detach();
    $('#new').append(objectWithEvents);
    

    Check the API docs here: http://api.jquery.com/detach/

提交回复
热议问题