【问题标题】:cloneNode without losing event listenercloneNode 不会丢失事件监听器
【发布时间】:2021-12-27 10:26:46
【问题描述】:

如何在不丢失附加到按钮的事件侦听器的情况下移动我的 html 元素?

克隆和移除原始元素后,子按钮的事件监听器不起作用

ul.appendChild(element.cloneNode(true));
element.remove();

【问题讨论】:

标签: javascript dom events dom-events


【解决方案1】:

你说过你想移动它,但你正在做的是克隆它,保存克隆,然后删除并丢弃原件。相反,不要克隆它,移动它:

ul.appendChild(element);

这将从其当前父级中删除 element 并将其放入其新父级 (ul) 中,而所有事件侦听器仍然存在。

现场示例:

// NOTE: This isn't how I'd write this code if I weren't demonstrating
// the fact the listeners are retained when the element is moved.
// But without context, it's hard to show a delegation solution.

const list1 = document.getElementById("list1");
const list2 = document.getElementById("list2");

// Add a listeneer to each `li`
document.querySelectorAll("li").forEach((li, index) => {
    li.addEventListener("click", () => {
        console.log(`Moving "${li.textContent}" which was originally at index ${index}`);
        if (li.closest("ul") === list1) {
            // Move from list1 to list2
            list2.appendChild(li);
        } else {
            // Move from list2 to list1
            list1.appendChild(li);
        }
    });
});
li {
    cursor: pointer;
}
<div>List 1:</div>
<ul id="list1">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>
<div>List 2:</div>
<ul id="list2">
    <li>Item 4</li>
    <li>Item 5</li>
    <li>Item 6</li>
</ul>
<div>Click an item to move it to the other list.</div>

也就是说,我经常发现事件委托在处理在父级之间移动的元素时是最好的,但这确实取决于具体情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2014-02-21
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多