所以我想交换两个内部有 CKEditor 的 div 元素。我查看了之前的一些问题并尝试这样做。没关系,元素被交换了。但是其中一个元素丢失了它的内容并且变得不可编辑。
<div id="sections">
<div id="apresentacao_div">
<label id="apresentacao_label" for="apresentacao">Apresentação</label>
<button class="btn btn-default">
<span class="glyphicon glyphicon-remove"></span>
</button>
<button class="btn btn-default">
<span class="glyphicon glyphicon-arrow-up"></span>
</button>
<CKEditor:CKEditorControl ID="apresentacao"></CKEditor:CKEditorControl>
</div>
<div id="intro_div">
<label id="intro_label" for="intro">Introdução</label>
<button class="btn btn-default" onclick="remove(this)">
<span class="glyphicon glyphicon-remove"></span>
</button>
<button class="btn btn-default" onclick="upDiv(this)">
<span class="glyphicon glyphicon-arrow-up"></span>
</button>
<CKEditor:CKEditorControl ID="intro"></CKEditor:CKEditorControl>
</div>
</div>
我想交换 id = "sections"的 div 中的两个 div。这是我要交换的代码:
function upDiv(ex) {
var div = document.getElementById("sections").getElementsByTagName("div");
for (i = 0; i < div.length; i = i + 4) {
if (div[i + 4].id.localeCompare(ex.parentNode.id) == 0) {
swapElements(div[i + 4], div[i]);
return false;
}
}
return false;
}
function swapElements(obj1, obj2) {
obj2.nextSibling === obj1 ? obj1.parentNode.insertBefore(obj2, obj1.nextSibling) : obj1.parentNode.insertBefore(obj2, obj1);
}
for 循环递增 4,因为将 textarea 转换为 CKEditor 添加了很多新的 div(在本例中为 4)。 谁能帮忙?
最佳答案
不太熟悉你在做什么,但我会尝试使用“克隆”方法,然后从页面上删除原始元素。可能是值没有被复制,只有大纲被复制,但克隆应该意味着它复制了页面上的内容,而不是页面源中的内容。
关于javascript - 交换 2 个 div 元素 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31297291/