jQuery bind click *ANYTHING* but *ELEMENT*

前端 未结 6 485
死守一世寂寞
死守一世寂寞 2020-12-02 07:24

Say there are some elements floating around, and I\'m trying to do some when I click ANYTHING(divs, body, whatever...) but the one specified (e.g. div#special).

I\'m

6条回答
  •  余生分开走
    2020-12-02 08:19

    You could also do

    $(document).bind('click', function(e) {
      if(!$(e.target).is('#special')) {
        // do something
      }
    });
    

    or if div#special has child elements you could do

    $(document).bind('click', function(e) {
      if($(e.target).closest('#special').length === 0) {
        // do something
      }
    });
    

提交回复
热议问题