How to do “If Clicked Else ..”

后端 未结 8 1572
灰色年华
灰色年华 2021-01-31 05:48

I am trying to use jQuery to do something like

if(jQuery(\'#id\').click) {
    //do-some-stuff
} else {
    //run function2
}

But I\'m unsure h

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 06:30

    You should avoid using global vars, and prefer using .data()

    So, you'd do:

    jQuery('#id').click(function(){
      $(this).data('clicked', true);
    });
    

    Then, to check if it was clicked and perform an action:

    if(jQuery('#id').data('clicked')) {
        //clicked element, do-some-stuff
    } else {
        //run function2
    }
    

    Hope this helps. Cheers

提交回复
热议问题