2012-03-14 43 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

相關問題