Click source in JavaScript and jQuery, human or automated?

后端 未结 4 2160
無奈伤痛
無奈伤痛 2020-12-11 18:40

We all know that you can simulate click or any other event on an element using one of these ways:

$(\'#targetElement\').trigger(\'eventName\');
$(\'#targetEl         


        
4条回答
  •  孤街浪徒
    2020-12-11 19:24

    In your immediate event handler, provide an e parameter. If the click is automated (via code), this e would be undefined (no need to check e.target as @alex has said):

    $('#targetElement').click(function(e){
       if(e)
       {
          // Click is triggered by a human action
       }
       else
       {
         // Click is triggered via code
       }
    });
    

提交回复
热议问题