gpt4 book ai didi

javascript - 通过纯 JavaScript 删除并替换 div 中的另一个 onclick 函数?

转载 作者:行者123 更新时间:2023-11-28 15:57:08 24 4
gpt4 key购买 nike

我在 span div 下有这个,并将其复制到 DOM 中的另一个位置。但是我需要删除 onclick 函数并放置另一个 onclick 函数。

<span class="plus childNode1" onclick="expand_collapseChildnodes('childNode1')" id="abc"></span>

在这里,我必须删除复制元素的 onclick="expand_collapseChildnodes('childNode1')" 并将其替换为 onclick="checkNodes('childNode1')" >.

最佳答案

考虑一个示例 HTML 页面:

<html>
<head>
</head>
<body>
<div id ="d1">
<span class="plus childNode1" onclick="expand_collapseChildnodes('childNode1')" id="abc"></span>
</div>
<div id ="d2">
</div>
</body>
</html>

现在将 ID 为 abc 的元素从 ID 为 d1 的 DOM 元素移动到 ID 为 d2 的另一个 DOM 元素

//get the element
var element = document.getElementById("abc");

//detach from source
document.getElementById("d1").removeChild(element);

//remove onclick attribute
element.removeAttribute("onclick");

//add the modified attribute
element.setAttribute("onclick", "sampleFunc()");

//attach the element to another source
document.getElementById("d2").appenChild(element);

关于javascript - 通过纯 JavaScript 删除并替换 div 中的另一个 onclick 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18331270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com