Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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>

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

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

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

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
417 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...