开发者

Moving a div into another div

开发者 https://www.devze.com 2022-12-15 18:30 出处:网络
i have 2 main div: <div id=\"d开发者_高级运维iv1\"> <div id=\"minidiv1\">a</div>

i have 2 main div:

    <div id="d开发者_高级运维iv1">
<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');

or

$('#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.

0

精彩评论

暂无评论...
验证码 换一张
取 消