简体   繁体   English

如何移动 DOM 中的元素?

[英]How to move an element in the DOM?

I have an element #superWidget in the DOM that is very interactive.我在 DOM 中有一个非常互动的元素 #superWidget。 It uses jquery UI sortable, draggable, and a bunch of other plugins.它使用 jquery UI 可排序、可拖动和一堆其他插件。

<div id="wrapperA"></div>
<div id="wrapperB">
  <div id="superWidget"></div>
</div>

I want to change the position of #superWidget in the DOM.我想在 DOM 中更改#superWidget 的 position。 I want to remove it from #wrapperB and place it in #wrapperA.我想将其从#wrapperB 中删除并将其放在#wrapperA 中。

<div id="wrapperA">
  <div id="superWidget"></div>
</div>
<div id="wrapperB"></div>

I have tried the following...我尝试了以下...

var copy = $("#superWidget").clone(true, true);
$("#superWidget").remove();
$("#wrapperA").append(copy);

...however this breaks a lot of the plugins. ...但是这破坏了很多插件。

I do not want to have to rebind everything.我不想重新绑定一切。 Is there a better way to do this?有一个更好的方法吗? (I notice that jquery UI sortable is somehow able to move elements around in the DOM without breaking any interactivity... there must be a way.) (我注意到 jquery UI sortable 能够以某种方式在 DOM 中移动元素而不会破坏任何交互性......必须有办法。)

Thanks (in advance) for your help在此先感谢您的帮助

而不是重复,只需这样做:

document.getElementById('wrapperA').appendChild(document.getElementById('superWidget'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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