javascript - Jquery bind click and hover, how to check if click - Stack Overflow

admin2025-03-20  6

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().

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1742420907a212930.html

最新回复(0)