I am using event.preventDefault()
to prevent concatenation of #
which is the href of an anchor to the URL. I am performing events on the mousedown()
and mouseup()
parts of the click which is why I can't use click. But event.preventDefault()
is not preventing the concatenation of #
to the URL when envoking开发者_运维问答 mouseup()
or mousedown()
methods. How can I get around this?
If you're talking about clicking a link, it is probably because there isn't a default behavior to prevent for mousedown
and mouseup
.
The default behavior of clicking a link requires a combination of mousedown
plus mouseup
on the link. If you mousedown
then drag off the link before you mouseup
, the link is not followed. The same vice-versa.
Only when you mousedown
then mouseup
is the default behavior activated. That event is represented by the click
event.
EDIT: I guess I forgot to answer the question.
How do you get around it? Add a click()
event handler that does e.preventDefault()
.
$('a.myElement').click(function(e){e.preventDefault()});
If you also want to stop propagation of the event, and if you're using jQuery 1.4.3 or later, you can do this:
$('a.myElement').bind('click',false);
From the docs for the bind()
(docs) method:
Setting the third argument to false will attach a function that prevents the default action from occurring and stops the event from bubbling.
Again, it requires jQuery 1.4.3
or later.
精彩评论