最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable click if class ="X", jQuery - Stack Overflow

programmeradmin9浏览0评论

Im trying to build a tabbed content box, and im wondering if its possible that i can disable 1 link with a specific class, such as 'disabled'

I read somewhere about a function called preventDefault, would this work?

/

Im trying to build a tabbed content box, and im wondering if its possible that i can disable 1 link with a specific class, such as 'disabled'

I read somewhere about a function called preventDefault, would this work?

http://jsfiddle/Ssr5W/

Share Improve this question asked Jan 27, 2012 at 10:39 LiamLiam 9,85540 gold badges114 silver badges214 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You can disable click event by returning false. like,

$('#tabmenu a').click(function() {
    return !$(this).hasClass('disabled');
});

Also, I've updated your fiddle: http://jsfiddle/Ssr5W/1/


EDITED

and of course, preventDefault would work :)

$('#tabmenu a').click(function(e) {
    if($(this).hasClass('disabled'))
        e.preventDefault();
});

fiddle: http://jsfiddle/Ssr5W/2/

$('.disabled').click(function(e) {
    e.preventDefault() ;
}) ;

You can just check for the class on the element that was clicked on:

$('tabElement').click(function(){
    if(this.hasClass('disabled'))
        return;
    //Your code here..
);

This won't interfere with other clikc-handlers you may have on your tab element

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论