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()
})
精彩评论