jQuery 禁用链接 [英] jQuery disable a link

查看:24
本文介绍了jQuery 禁用链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在不使用 return false; 的情况下禁用 jquery 中的链接吗?

Anyone know how to disable a link in jquery WITHOUT using return false;?

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

Specifically, what I'm trying to do is disable the link of an item, performing a click on it using jquery which triggers some stuff, then re-enabling that link so that if it's clicked again it works as default.

谢谢.戴夫

更新这是代码.应用.expanded 类后需要做的是重新启用禁用的链接.

UPDATE Here's the code. What it needs to do after the .expanded class has been applied is to re-enable the disabled link.

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

推荐答案

$('#myLink').click(function(e) {
    e.preventDefault();
    //do other stuff when a click happens
});

这将阻止超链接的默认行为,即访问指定的 href.

That will prevent the default behaviour of a hyperlink, which is to visit the specified href.

来自 jQuery 教程:

From the jQuery tutorial:

对于点击和大多数其他事件,您可以防止默认行为 -在这里,按照 jquery.com 的链接- 通过在事件处理程序中调用 event.preventDefault()

For click and most other events, you can prevent the default behaviour - here, following the link to jquery.com - by calling event.preventDefault() in the event handler

如果您想preventDefault() 仅在满足特定条件时(例如隐藏某些内容),您可以使用 expanded 类测试您的 ul 的可见性.如果它是可见的(即未隐藏),则链接应正常触发,因为不会输入 if 语句,因此不会阻止默认行为:

If you want to preventDefault() only if a certain condition is fulfilled (something is hidden for instance), you could test the visibility of your ul with the class expanded. If it is visible (i.e. not hidden) the link should fire as normal, as the if statement will not be entered, and thus the default behaviour will not be prevented:

$('ul li').click(function(e) {
    if($('ul.expanded').is(':hidden')) {
        e.preventDefault();
        $('ul').addClass('expanded');
        $('ul.expanded').fadeIn(300);
    } 
});

这篇关于jQuery 禁用链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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