首先单击event.preventDefault(),然后将其删除 [英] event.preventDefault() on first click then remove

查看:73
本文介绍了首先单击event.preventDefault(),然后将其删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果它具有class subnav,则我禁用了默认锚,如此小提琴所示./p>

我只希望第一次单击时禁用此功能,然后希望恢复正常的锚点功能.做这个的最好方式是什么?我尝试了涉及以下代码的操作,但这似乎不起作用?

  $(this).unbind(event.preventDefault());

也许是这样的伪代码?

if (click count === 0 ) {
    event.preventDefault();
}

还是有更好的方法来解决这个问题?

解决方案

您可以将false传递给 one() :

$(".subnav a").one("click", false);

传递false而不是处理程序等同于传递返回false的处理程序,从而有效地阻止了事件的传播并防止了其默认行为.

bind()的文档中对此进行了解释:

在jQuery 1.4.3中,您现在可以传递false代替事件 处理程序.这将绑定等同于function() { return false; }.

的事件处理程序.

I have the default anchor disabled if it has a class subnav as shown in this fiddle.

I only want this disabled for the first click then I want the normal anchor functionality to be brought back. What is the best way to do this? I tried something involving the below code but this didn't seem to work?

  $(this).unbind(event.preventDefault());

maybe something like this psuedo code?

if (click count === 0 ) {
    event.preventDefault();
}

or is there a better way to approach this?

解决方案

You can pass false to one():

$(".subnav a").one("click", false);

Passing false instead of a handler is equivalent to passing a handler that returns false, effectively stopping the event's propagation and preventing its default behavior.

This is explained in the documentation for bind():

In jQuery 1.4.3 you can now pass in false in place of an event handler. This will bind an event handler equivalent to: function() { return false; }.

这篇关于首先单击event.preventDefault(),然后将其删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆