将一个部门的子元素以及所有关联事件复制到另一个部门
我正在尝试将某个 div 元素 parent
替换为另一个 newparent
。我只想复制 parent
的一些子级并将它们放入 newparent
中,然后用 newparent
替换 parent
代码>. 这是我的代码片段:
var sb_button = parent.firstChild;
var temp;
while(sb_button) {
console.log("loop: ");
console.log(sb_button.id);
temp = sb_button;
if(sb_button.id != curr_button.id && sb_button.id != prev_button.id) {
console.log("if");
newparent.appendChild(temp);
}
else if(sb_button.id == curr_button.id) {
console.log("elseif");
newparent.appendChild(temp);
newparent.appendChild(prev_button);
}
else {
console.log("else");
}
sb_button.parentNode = parent;
console.log(sb_button.id)
console.log(sb_button.parentNode.children);
sb_button = sb_button.nextSibling;
}
parent.parentNode.replaceChild(newparent,parent);
编辑:
所以当我执行 newparent.appendChild(temp)
时,它会修改 sb_button
。有什么解决方法吗?
I am trying to replace a certain div element parent
with another one newparent
. I want to copy only some of parent
's children and put them in newparent
, and then replace the parent
by newparent
.
Here is a snippet of my code:
var sb_button = parent.firstChild;
var temp;
while(sb_button) {
console.log("loop: ");
console.log(sb_button.id);
temp = sb_button;
if(sb_button.id != curr_button.id && sb_button.id != prev_button.id) {
console.log("if");
newparent.appendChild(temp);
}
else if(sb_button.id == curr_button.id) {
console.log("elseif");
newparent.appendChild(temp);
newparent.appendChild(prev_button);
}
else {
console.log("else");
}
sb_button.parentNode = parent;
console.log(sb_button.id)
console.log(sb_button.parentNode.children);
sb_button = sb_button.nextSibling;
}
parent.parentNode.replaceChild(newparent,parent);
EDIT :
So when I do newparent.appendChild(temp)
it modifies sb_button
. What's the workaround for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有运行你的代码,但有一些奇怪的事情,也许其中之一可能会导致问题或帮助清理代码,使问题更加明显。
temp
似乎是sb_button
的别名:您可以删除变量声明并将所有引用替换为temp
sb_button 是一个令人困惑的名称,对于
sb_button
中的节点附加到newparent
的任意子节点来说,但在您尝试设置之后sb_button_.parentNode
到parent
- 即 不可能,因为parentNode
是只读的,而且它当然没有意义 - 您不能将元素附加到一个元素,但有一个不同的元素父母。编辑:鉴于您想要复制节点,我相信您正在寻找
cloneNode
:制作节点的副本并附加该副本,而不是原始节点。作为简洁设计的问题,当事情变得复杂时,我会避免这种难以推理的 while 循环。相反,只需创建一个节点数组,按照您想要的方式对它们进行排序(您甚至可以使用
sort
来执行此操作,以使其立即明显地看出您只是在重新排列事物),然后创建一个函数接受newparent
和数组,并将所有元素的副本按数组顺序附加到newparent
。您的示例并不那么复杂,但即使在这里,我也会更改 if 子句的顺序,以便在最后的 else 中使用“默认”情况。例如:这个想法是让读者立即明白所有子项都被处理一次。
I haven't run your code, but there's a few weird things, perhaps one of which may cause the issue or help clear up the code so the issue is more obvious.
temp
seems to be an alias forsb_button
: you could remove the variable declaration and replace all references withtemp
sb_button
is a confusing name for an arbitrary child nodesb_button
tonewparent
within the if statement, but right after you're trying to setsb_button_.parentNode
toparent
- that's not possible sinceparentNode
is readonly and it certainly doesn't make sense - you can't append the element to one element but have a different parent.Edit: given that you want to copy nodes, I believe you're looking for
cloneNode
: make a copy of the node and append that copy, not the original node.As a matter of clean design, when things get complicated, I'd avoid this kind of hard-to-reason-about while loop. Instead, simply make an array of the nodes, order those the way you want (you could even do this using
sort
to make it immediately obvious you're just rearranging things), and then make a function that takes anewparent
and the array and appends copies of all elements in array order tonewparent
. Your example isn't that complex, but even here, I'd change the order of the if-clauses to have the "default" case in the final else. e.g.:The idea being to make it instantly obvious to the reader that all children are processed exactly once.