admin管理员组文章数量:1426222
I have a div #someDiv, can I create a new element and make #someDiv be its child? Basically I want to wrap a new element aound #someDiv. How do I do this?
$("#someDiv").appendTo($("<div id='newParent'></div>"));
// The intent is to move #someDiv into the new container
// The new container may or may not be in the same
// position in the DOM as #someDiv
Thanks for any tips!
Cheers, ~ck in San Diego
I have a div #someDiv, can I create a new element and make #someDiv be its child? Basically I want to wrap a new element aound #someDiv. How do I do this?
$("#someDiv").appendTo($("<div id='newParent'></div>"));
// The intent is to move #someDiv into the new container
// The new container may or may not be in the same
// position in the DOM as #someDiv
Thanks for any tips!
Cheers, ~ck in San Diego
Share Improve this question asked Jan 9, 2010 at 4:40 HcabnettekHcabnettek 12.9k38 gold badges129 silver badges192 bronze badges 2- Is there any error with the above code? – rahul Commented Jan 9, 2010 at 4:45
- nope. not syntactically, leastways. – jrharshath Commented Jan 9, 2010 at 4:48
5 Answers
Reset to default 2$("#something").wrap("<div id='newParent'></div>");
$("#someDiv").wrap("<div id='newParent'></div>");
Sure. You can do anyting in jquery. check out jQuery.wrap
Assuming someDiv is already appended to the document...
$('#someDiv').wrap('<div>')
This would wrap the element with a newly created division. Documentation.
if the newParent div is not created you'll want to use wrap.
$('somediv').wrap("<div id='newparent'></div>");
if it already exists, what you have will work.
本文标签:
版权声明:本文标题:javascript - jQuery - can I dynamically change an elements parent? A a new element and make it the parent of a selection? - Stac 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745461593a2659347.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论