以编程方式删除按钮上的事件侦听器

Kir*_*ran 4 javascript javascript-events

我有一个Button,它注册了一个onclick事件,如图所示

<Input type="Button" name="Register" Value="Register" onclick="CallMe();"/>
Run Code Online (Sandbox Code Playgroud)

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

Gab*_*oli 9

您可以将onclick属性设置为null.

var el = document.getElementById('inputId');
el.onclick = null;
Run Code Online (Sandbox Code Playgroud)

或者更好的是只使用元素的removeAttribute() docs方法删除该属性.

var el = document.getElementById('inputId');
el.removeAttribute('onclick');
Run Code Online (Sandbox Code Playgroud)

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

http://jsfiddle.net/gaby/PBjtZ/上的示例代码