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

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

问题描述

任何人都知道如何在jquery中禁用链接而不使用 return false;

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.

谢谢。
Dave

Thanks. Dave

UPDATE
这是代码。应用 .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 教程


对于点击和大多数其他事件,你
可以阻止默认行为 -
这里,链接到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( )只有在满足某个条件(例如隐藏某些条件)时,您才可以使用扩展类来测试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天全站免登陆