Exclude click event based on condition?

前端 未结 5 1270
悲&欢浪女
悲&欢浪女 2021-02-14 04:38

I\'m trying to set conditions on a jQuery event. I have an input element and a div. I want to detect a click anywhere on the page and to execute that method but only if the clic

5条回答
  •  长情又很酷
    2021-02-14 05:11

    var input = $("#yourInput")[0];
    var div = $("#yourDiv")[0];
    
    $(document.body).click(function(e) {
       switch ( e.target ) {
           case input: case div: return;
       }
       yourMethod();
    });
    

提交回复
热议问题