有人知道如何在jquery中禁用一个链接而不使用return false吗?

具体来说,我要做的是禁用一个项目的链接,使用jquery执行点击它触发一些东西,然后重新启用该链接,以便如果它再次单击它作为默认工作。

谢谢。 戴夫

更新 这是代码。在应用.expanded类之后,它需要做的是重新启用被禁用的链接。

$('ul li').click(function(e) {
    e.preventDefault();
    $('ul').addClass('expanded');
    $('ul.expanded').fadeIn(300);
    //return false;
});

当前回答

我知道这不是用jQuery,但你可以用一些简单的css禁用一个链接:

a[disabled] {
  z-index: -1;
}

HTML看起来是这样的

<a disabled="disabled" href="/start">Take Survey</a>

其他回答

你可以隐藏和显示你喜欢的链接

$(link).hide();
$(link).show();

如果您使用href路径,则可以保存它

禁用:

$('a').each(function(){
    $(this).data("href", $(this).attr("href")).removeAttr("href");
});

然后重新启用使用:

$('a').each(function(){
    $(this).attr("href", $(this).data("href"));
});

在一种情况下,我不得不这样做,因为点击事件已经绑定到其他地方,我无法控制它。

我总是用这个在jQuery禁用链接

$("form a").attr("disabled", "disabled");

这适用于内联设置了onclick属性的链接。这也允许您稍后删除“返回false”以启用它。

        //disable all links matching class
        $('.yourLinkClass').each(function(index) {
            var link = $(this);
            var OnClickValue = link.attr("onclick");
            link.attr("onclick", "return false; " + OnClickValue);
        });

        //enable all edit links
        $('.yourLinkClass').each(function (index) {
            var link = $(this);
            var OnClickValue = link.attr("onclick");
            link.attr("onclick", OnClickValue.replace("return false; ", ""));
        });

HTML链接示例:

        <!-- boostrap button + fontawesome icon -->
        <a class="btn btn-primary" id="BT_Download" target="_blank" href="DownloadDoc?Id=32">
        <i class="icon-file-text icon-large"></i>
        Download Document
        </a>

在jQuery中使用

    $('#BT_Download').attr('disabled',true);

添加到CSS:

    a[disabled="disabled"] {
        pointer-events: none;
    }