javascript怎样移除onclick事件

文章2022-05-08149 人已阅来源:网络

javascript移除onclick事件的方法:1、使用“document.getElementById("id值")”语句根据id值获取元素对象;2、使用“元素对象.onclick=null;”语句来移除onclick事件即可。

javascript怎样移除onclick事件

本教程操作环境:windows10系统、javascript1.8.5版、Dell G3电脑。

javascript怎样移除onclick事件

可以使用“document.getElementById("id值")”语句根据id值获取元素对象;然后使用“元素对象.onclick=null;”语句来移除onclick事件。

移除元素onclick事件示例如下:

<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script>
function myFunction(){
document.getElementById("mybutton").onclick=null;
document.getElementById("demo").innerHTML="Hello World";
}
</script>
</head>
<body>
<p>单击按钮触发函数。</p>
<button onclick="myFunction()" id="myburtton">点我</button>
<p id="demo"></p>
</body>
</html>

输出结果:

javascript怎样移除onclick事件

(学习视频分享:css视频教程)

以上就是javascript怎样移除onclick事件的详细内容!