Move div to other parent, so it inherits from that parent
Possible Duplicate:
How to move an element into another element?
I basically want to reassign a parent to my DIV:
<DIV id='main'>
<DIV id='child'>
</DIV>
</DIV>
<DIV id='surrogate'>
</DIV>
So i want child to be child of surrogate.
I tried:
var $a = $('#child');
var contents = $a.contents();
$a.remove();
$('#surrogate').append('<p>' + contents + '</p>');
But it will just output: [object Object]
Is there a better/working way to just reassign a parent to a whole tree of elements, without reading the content copying or cloning it? Well, maybe cloning works, but I'd rather just move it.
You want to get the html from inside child which returns a string so you can concatenate it the way you are doing
var $a = $('#child');
$('#surrogate').append('<p>' + $a.html() + '</p>');
$a.remove();
contents() is returning a jQuery object and you can't concatenate an object and string
链接地址: http://www.djcxy.com/p/42534.html上一篇: 如何将锚标签从一个地方附加到另一个地方?
下一篇: 将div移到其他父项,所以它从该父项继承