将一个部门的子元素以及所有关联事件复制到另一个部门

发布于 2024-11-13 06:39:34 字数 1155 浏览 2 评论 0原文

我正在尝试将某个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2024-11-20 06:39:34

我还没有运行你的代码,但有一些奇怪的事情,也许其中之一可能会导致问题或帮助清理代码,使问题更加明显。

  • 变量 temp 似乎是 sb_button 的别名:您可以删除变量声明并将所有引用替换为 temp
  • sb_button 是一个令人困惑的名称,对于
  • 您在 if 语句中将 sb_button 中的节点附加到 newparent 的任意子节点来说,但在您尝试设置之后sb_button_.parentNodeparent - 即 不可能,因为 parentNode 是只读的,而且它当然没有意义 - 您不能将元素附加到一个元素,但有一个不同的元素父母。
  • 您是否正在尝试复制或移动节点?

编辑:鉴于您想要复制节点,我相信您正在寻找 cloneNode:制作节点的副本并附加该副本,而不是原始节点。

作为简洁设计的问题,当事情变得复杂时,我会避免这种难以推理的 while 循环。相反,只需创建一个节点数组,按照您想要的方式对它们进行排序(您甚至可以使用 sort 来执行此操作,以使其立即明显地看出您只是在重新排列事物),然后创建一个函数接受 newparent 和数组,并将所有元素的副本按数组顺序附加到 newparent。您的示例并不那么复杂,但即使在这里,我也会更改 if 子句的顺序,以便在最后的 else 中使用“默认”情况。例如:

for(var child = parent.firstChild; child; child = child.nextSibling) 
    if(child.id == curr_button.id) { //insert prev_button after curr_button
        newparent.appendChild(child.cloneNode(true));
        newparent.appendChild(prev_button.cloneNode(true));
    } else if(child.id != prev_button.id) {
        newparent.appendChild(child.cloneNode(true));
    }
parent.parentNode.replaceChild(newparent, parent);

这个想法是让读者立即明白所有子项都被处理一次。

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.

  • the variable temp seems to be an alias for sb_button: you could remove the variable declaration and replace all references with temp
  • sb_button is a confusing name for an arbitrary child node
  • you're appending the node in sb_button to newparent within the if statement, but right after you're trying to set sb_button_.parentNode to parent - that's not possible since parentNode is readonly and it certainly doesn't make sense - you can't append the element to one element but have a different parent.
  • are you trying to copy or move nodes?

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 a newparent and the array and appends copies of all elements in array order to newparent. 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.:

for(var child = parent.firstChild; child; child = child.nextSibling) 
    if(child.id == curr_button.id) { //insert prev_button after curr_button
        newparent.appendChild(child.cloneNode(true));
        newparent.appendChild(prev_button.cloneNode(true));
    } else if(child.id != prev_button.id) {
        newparent.appendChild(child.cloneNode(true));
    }
parent.parentNode.replaceChild(newparent, parent);

The idea being to make it instantly obvious to the reader that all children are processed exactly once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文