admin管理员组

文章数量:1313176

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

本文标签: javascriptDisable click if class quotXquotJQueryStack Overflow