I am using event.preventDefault()
to prevent concatenation of #
which is the href of an anchor to the URL. I am preforming events on the mousedown() and mouseup() parts of the click which is why i cant 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?
我正在使用event.preventDefault()防止将#连接到URL的href。我正在对mousedown()和mouseup()部分的单击进行事件预置,这就是我不能使用click的原因。但是event.preventDefault()并不妨碍在envoking mouseup()或mousedown()方法时将#连接到URL。我怎样才能避开这个问题呢?
1 个解决方案
#1
11
If you're talking about clicking a link, it is probably because there isn't a default behavior to prevent for mousedown
and mouseup
.
如果您正在讨论单击一个链接,可能是因为没有默认行为来防止mousedown和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()
.
你如何避开它?添加一个click()事件处理程序。
$('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:
如果您还想停止事件的传播,如果您正在使用jQuery 1.4.3或更高版本,您可以这样做:
$('a.myElement').bind('click',false);
From the docs for the bind()
(docs) method:
绑定()(docs)方法的文档:
Setting the third argument to false will attach a function that prevents the default action from occurring and stops the event from bubbling.
将第三个参数设置为false将附加一个函数,该函数阻止默认操作的发生,并阻止事件冒泡。
Again, it requires jQuery 1.4.3
or later.
同样,它需要jQuery 1.4.3或更高版本。
#1
11
If you're talking about clicking a link, it is probably because there isn't a default behavior to prevent for mousedown
and mouseup
.
如果您正在讨论单击一个链接,可能是因为没有默认行为来防止mousedown和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()
.
你如何避开它?添加一个click()事件处理程序。
$('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:
如果您还想停止事件的传播,如果您正在使用jQuery 1.4.3或更高版本,您可以这样做:
$('a.myElement').bind('click',false);
From the docs for the bind()
(docs) method:
绑定()(docs)方法的文档:
Setting the third argument to false will attach a function that prevents the default action from occurring and stops the event from bubbling.
将第三个参数设置为false将附加一个函数,该函数阻止默认操作的发生,并阻止事件冒泡。
Again, it requires jQuery 1.4.3
or later.
同样,它需要jQuery 1.4.3或更高版本。