Exclude click event based on condition?

前端 未结 5 1260
悲&欢浪女
悲&欢浪女 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:00

    Basically assign a click handler to the body of the page, then test the target Element of the event to see if the id of that target element matches your div or your input.

    $("body").click(function(evt) {
      var targetElementId = evt.target.id;
    
      if (!(targetElementId  == "yourDivID" || targetElementId == "yourinputId"))
      {
        // your event code here.
      }
    });
    

提交回复
热议问题