通过Pure Javascript删除并替换div中的另一个onclick函数? [英] Remove and replace another onclick function in a div through Pure Javascript?

查看:61
本文介绍了通过Pure Javascript删除并替换div中的另一个onclick函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this below span div and I copy this to another place in the DOM. But the onclick function I need to remove and place another onclick function.

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

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

Here I have to remove the onclick="expand_collapseChildnodes('childNode1')" for the copied element and replace it with onclick="checkNodes('childNode1')"

推荐答案

考虑一个示例HTML页面:

Consider a sample HTML page:

<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 的另一个元素

Now to move the element with id abc from DOM element with id d1 to another with id d2

//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);

这篇关于通过Pure Javascript删除并替换div中的另一个onclick函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆