简体   繁体   中英

Moving a div into another div

i have 2 main div:

    <div id="div1">
<div id="minidiv1">a</div>
<div id="minidiv2">b</div>
   </div>
   <div id="div2"></div>

I want move the minidiv1 into the div2 with jquery

how can i do?

You can simply append it to the div2 , and it will change its location in the DOM:

$('#minidiv1').appendTo('#div2'); 
// or
$('#div2').append('#minidiv1');

The difference of the above two lines is what is returned, appendTo returns the #minidiv element, append will return the #div2 element.

Use the one you find most useful if you want to make more actions (by chaining).

$('#minidiv1').appendTo('#div2');

要么

$('#div2').append($('#minidiv1'));
$("#minidiv1").appendTo("#div2")

Luca, did Paul's answer actually work for you? I think you need to be referencing the jQuery object like so:

$($('#minidiv1')).appendTo('#div2'); 
// or
$('#div2').append($('#minidiv1'));

Otherwise, jQuery will just append the string "#minidiv1" to #div2, rather than moving your existing div.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM