click on body except some other tag not working

与世无争的帅哥 提交于 2019-12-10 11:28:38

问题


does anyone know if css position:relative; can mess up the function

    $('body').not($('.theDIV')).click(function(){
    alert('');

    });

or the problem is somewhere else?

what is happening is i have a that appears on click of a button and i want it to hide() when i click anywhere on the body, except the div itself. HTML

<ul class='messages'> //these are made dynamically. should i use each() to go through all the elements?
<li>
    <div class='theDIV'></div>
    <input type='button'>
</li>
<li>
    <div class='theDIV'></div>
    <input type='button'>
</li>
<li>
    <div class='theDIV'></div>
    <input type='button'>
</li>
</ul>

sorry if i wasnt clear the first time


回答1:


You could do

$('body').click(function(e){
   if(! $(e.target).hasClass('theDIV')){
     alert('clicked on something that has not the class theDIV');
   }

});


来源:https://stackoverflow.com/questions/9711134/click-on-body-except-some-other-tag-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!