解绑内联onClick不能在jQuery中工作? [英] Unbinding inline onClick not working in jQuery?

查看:131
本文介绍了解绑内联onClick不能在jQuery中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我想知道如何在jQuery中取消绑定内联onclick事件。您认为 .unbind()可行,但事实并非如此。

Okay so, I'm wondering how to unbind an inline onclick event in jQuery. You'd think .unbind() would work, however it doesn't.

要自己测试一下,请使用以下HTML和JavaScript:

To test this for yourself, play around with the following HTML and JavaScript:

function UnbindTest() {
    $("#unbindTest").unbind('click');
}

function BindTest() {
    $("#unbindTest").bind('click', function() { alert("bound!"); });
}


<button type="button" onclick="javascript:UnbindTest();">Unbind Test</button>
<button type="button" onclick="javascript:BindTest();">Bind Test</button>
<button type="button" onclick="javascript:alert('unbind me!');" id="unbindTest">Unbind Button</button>

正如您所看到的,取消绑定不解除内联 onclick event ...但它解除了与 bind()一起添加的click事件的绑定。

As you can see, unbinding does not unbind the inline onclick event... however it does unbind the click event added with bind().

所以,我想知道是否有办法在不执行以下操作的情况下解除内联onclick事件:

So, I'm wondering if there is a way to unbind inline onclick events short of doing the following:

$("#unbindTest").get(0).onclick = "";

谢谢

推荐答案

jQuery的unbind不适用于 onclick 属性 - 它只适用于通过 bind 因此可在 $(...)。data('events')中使用。您必须使用 removeAttr 删除 onclick

jQuery's unbind won't work on onclick attributes - it only works for functions that were added via bind and thus are available in $(...).data('events'). You have to use removeAttr to remove onclick.

有关详细信息,请参阅此问题

Read this question for more info.

这篇关于解绑内联onClick不能在jQuery中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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