admin管理员组

文章数量:1336660

I have bined function like this(simplified version):

$('label').bind('click hover', function() {
    $('label').removeClass("active");
    $(this).addClass("active");
});

How can I add an if to check if it is a click?

I have bined function like this(simplified version):

$('label').bind('click hover', function() {
    $('label').removeClass("active");
    $(this).addClass("active");
});

How can I add an if to check if it is a click?

Share Improve this question asked Mar 21, 2012 at 22:35 John MagnoliaJohn Magnolia 16.8k39 gold badges169 silver badges274 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Use event.type:

$('label').bind('click hover', function(event) {
    if(event.type == 'click') {
        // do stuff
    }
    $('label').removeClass("active");
    $(this).addClass("active");
});

Demo: http://jsfiddle/jtbowden/sqvDy/

Note, the demo uses .on(), which is the new method of event binding for jQuery 1.7+, replacing .bind(), .live(), and .delegate().

本文标签: javascriptJquery bind click and hoverhow to check if clickStack Overflow