以编程方式删除按钮上的事件侦听器 [英] Removing the Event Listener on a Button programatically

查看:62
本文介绍了以编程方式删除按钮上的事件侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册了 onclick 事件的按钮,如图所示

I have a Button which is registered with a onclick event as shown

<Input type="Button" name="Register" Value="Register" onclick="CallMe();"/>

是否可以以编程方式删除或取消注册此按钮上的onclick事件?

Is it possible to programatically remove or deregister the onclick event on this Button?

推荐答案

您可以将onclick属性设置为 null

You could set the onclick attribute to null.

var el = document.getElementById('inputId');
el.onclick = null;

或更好只需使用 removeAttribute() docs 元素的方法。

or better just remove the attribute altogether with the removeAttribute() docs method of the element.

var el = document.getElementById('inputId');
el.removeAttribute('onclick');

你需要为元素添加一个id,但是这个..

you will need to add an id to the element though for this..

http://jsfiddle.net/gaby/PBjtZ/

这篇关于以编程方式删除按钮上的事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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