Exclude click event based on condition?

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

    Use the jQuery click() or live() functions and then check the target of the click event using the jQuery is() function.

    1. bind click event on document
    2. if the target is not input or div continue

    .

    $(document.body).click(function(e) {
      if( !$(e.target).is("input, div") ) {
        console.log("Run function because image or div were not clicked!");
      }
    });
    

    Example webpage => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-document-click-exclusion.html

    Example firebug output after clicking around on example page

    alt text

提交回复
热议问题