如何使用jQuery启用或禁用锚点? [英] How to enable or disable an anchor using jQuery?

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

问题描述

如何使用jQuery启用或禁用锚点?

解决方案

为了防止锚点遵循指定的 href ,我会建议使用 preventDefault()

  // jQuery 1.7+ 
$(function(){
$ ('a.something')。on(click,function(e){
e.preventDefault();
});
});

// jQuery< 1.7
$(function(){
$('a.something')。click(function(e){
e.preventDefault();
});

//或

$('a.something')。bind(click,function(e){
e.preventDefault();
});
});

见:

http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29



另请参阅上一个关于SO的问题:

jQuery禁用链接


How to enable or disable an anchor using jQuery?

解决方案

To prevent an anchor from following the specified href, I would suggest using preventDefault():

// jQuery 1.7+
$(function () {
    $('a.something').on("click", function (e) {
        e.preventDefault();
    });
});

// jQuery < 1.7
$(function () {
    $('a.something').click(function (e) {
        e.preventDefault();
    });

    // or 

    $('a.something').bind("click", function (e) {
        e.preventDefault();
    });
});

See:

http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29

Also see this previous question on SO:

jQuery disable a link

这篇关于如何使用jQuery启用或禁用锚点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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