jQuery check if it is clicked or not

后端 未结 6 1332
失恋的感觉
失恋的感觉 2020-11-29 02:19
$( element ).click( function() {

});

How can I check if the element is clicked or not? I\'m doing like that

function element() {
         


        
6条回答
  •  被撕碎了的回忆
    2020-11-29 02:34

    Alright, before I go into the solution, lets be on the same line about this one fact: Javascript is Event Based. So you'll usually have to setup callbacks to be able to do procedures.

    Based on your comment I assumed you have a trigger that will do the logic that launched the function depending if the element is clicked; for sake of demonstration I made it a "submit button"; but this can be a timer or something else.

    var the_action = function(type) {
        switch(type) {
            case 'a':
                console.log('Case A');
                break;
             case 'b':
                console.log('Case B');
                break;
        }
    };
    
    $('.clickme').click(function() { 
        console.log('Clicked');
        $(this).data('clicked', true);
    });
    
    $('.submit').click(function() {
        // All your logic can go here if you want.
        if($('.clickme').data('clicked') == true) {
            the_action('a');
        } else {
            the_action('b');
        }
    });
    

    Live Example: http://jsfiddle.net/kuroir/6MCVJ/

提交回复
热议问题