开发者

How to unbind a hyperlink with jQuery

Is it possible to unbind the default behavior of a hyperlink?

I've learned from other questions that unbind can only be used on functions being bound with jQuery. I guess this could be 开发者_高级运维the reason for it not working.

jsFiddle


You can prevent the default behavior using event.preventDefault, or by returning false from an event handler.

$('#some-link-id').click(function (event)
{
    event.preventDefault();
});

// or

$('#some-link-id').click(function ()
{
    return false;
});

Demo →

As you suspected, you cannot use unbind() because there is no listener to unbind.


$('myDiv').unbind('click') - if it was hooked up via JavaScript

$('myDiv').attr('onclick','') - if it has an inline event

$('myDiv').attr('href','javascript://') - creates a dummy link that does nothing


$("#buttonId").click(function() { return false; });


you could try to bind all event types to it and use event.preventDefault() to stop default behavior something like this:

$('a').bind('touchstart touchmove touchend mousedown click mouseup select',function(e){
e.preventDefault()
})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜