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

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

谢谢。 戴夫

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

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

当前回答

我最喜欢的是“签出编辑项目和防止-狂野西部点击到任何地方-在签出”功能

$('a').click(function(e) {
    var = $(this).attr('disabled');
    if (var === 'disabled') {
        e.preventDefault();
    }
});

因此,如果我想在第二个动作工具栏中禁用所有外部链接,而在“编辑模式”如上所述,我将添加编辑功能

$('#actionToolbar .external').attr('disabled', true);

火灾后链路示例:

<a href="http://goo.gl" target="elsewhere" class="external" disabled="disabled">Google</a>

现在你可以使用禁用属性链接

干杯!

其他回答

这适用于内联设置了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;
    }

对于像我这样通过谷歌来到这里的人,这里有另一种方法:

css:
.disabled {
  color: grey; // ...whatever
}

jQuery:
$('#myLink').click(function (e) {
  e.preventDefault();
  if ($(this).hasClass('disabled'))
    return false; // Do something else in here if required
  else
    window.location.href = $(this).attr('href');
});

// Elsewhere in your code
if (disabledCondition == true)
  $('#myLink').addClass('disabled')
else
  $('#myLink').removeClass('disabled')

记住:这不仅是一个css类

类=“按钮样式”

还有这两个

类= " buttonstyle禁用"

所以你可以很容易地添加和删除更多的类与jQuery。没必要碰它…

我爱jQuery!: -)

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

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

HTML看起来是这样的

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

只要触发东西,设置标志,返回false。如果设置了flag -什么都不做。