将div移动到dom中的其他位置 [英] Move a div somewhere else in the dom
本文介绍了将div移动到dom中的其他位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码被动态插入到DOM中.但是,我想将div#example从它所在的位置移到它前面,并将其添加到#wrapper中.如何使用jQuery实现此目标?
The following code gets dynamically inserted into the DOM. However, I'd like to move div#example from where it is and prepend it to #wrapper. How can I use jQuery to achieve this?
<div id="wrapper">
<div id="div1">
<div id="example">
</div>
</div>
<div id="div2">
</div>
</div>
我尝试了$('#wrapper').prepend('#example');
但这只是将文本#example
(不是div)添加到#wrapper中.
But that just adds the text #example
(not the div) into #wrapper.
推荐答案
您可以做到
$('#wrapper').prepend( $('#example') );
或
$('#example').prependTo('#wrapper');
这篇关于将div移动到dom中的其他位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文