2012-03-14 37 views
2

没有人知道,如果CSS位置:相对;可以搞砸功能点击身体,除了一些其他标签不工作

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

    }); 

或者问题出在别的地方?

发生了什么是我有一个出现在点击按钮上,我希望它隐藏()当我点击身体上的任何地方,除了div本身。 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> 

对不起,如果我不是清除首次

+0

为什么你在每一个这样做呢? – 2012-03-14 22:16:43

+0

你的html代码是什么?即使你有2 $('。messages li'),它们也会互相混淆.not()。你究竟在做什么? – 2012-03-14 22:34:01

+0

代码没有意义,身体永远不可能是任何LI的后代,所以'不'是没有价值的,并且你正在为每个元素添加新的点击处理程序到身体 – charlietfl 2012-03-14 23:16:56

回答

5

你可以做

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

}); 
+0

感谢您的解决方案。这是TIMESAVER! – Simon 2014-05-08 15:04:34