How to move an element into another element?

前端 未结 15 2263
無奈伤痛
無奈伤痛 2020-11-22 03:35

I would like to move one DIV element inside another. For example, I want to move this (including all children):

...
15条回答
  •  心在旅途
    2020-11-22 04:19

    Old question but got here because I need to move content from one container to another including all the event listeners.

    jQuery doesn't have a way to do it but standard DOM function appendChild does.

    //assuming only one .source and one .target
    $('.source').on('click',function(){console.log('I am clicked');});
    $('.target')[0].appendChild($('.source')[0]);
    

    Using appendChild removes the .source and places it into target including it's event listeners: https://developer.mozilla.org/en-US/docs/Web/API/Node.appendChild

提交回复
热议问题