How to move child element from one parent to another using jQuery

This question already has an answer here:

  • How to move an element into another element? 12 answers

  • $('#parent2').prepend($('#table1_length')).prepend($('#table1_filter'));

    doesn't work for you? I think it should...


    As Jage's answer removes the element completely, including event handlers and data, I'm adding a simple solution that doesn't do that, thanks to the detach function.

    var element = $('#childNode').detach();
    $('#parentNode').append(element);
    

    Edit:

    Igor Mukhin suggested an even shorter version in the comments below:

    $("#childNode").detach().appendTo("#parentNode");


    Detach is unnecessary.

    The answer (as of 2013) is simple:

    $('#parentNode').append($('#childNode'));
    

    According to http://api.jquery.com/append/

    You can also select an element on the page and insert it into another:

    $('.container').append($('h2'));

    If an element selected this way is inserted into a single location elsewhere in the DOM, it will be moved into the target (not cloned).

    链接地址: http://www.djcxy.com/p/42532.html

    上一篇: 将div移到其他父项,所以它从该父项继承

    下一篇: 如何使用jQuery将子元素从一个父元素移动到另一个父元素