2010-06-15 56 views
2

我有頁的一部分這樣JQuery的翻轉表選擇

<div id="inbox"> 
    <h1>Headline</h1> 
    <p>First paragraph</p> 
    <table> 
    <tbody> 
     <tr> 
     <td>table stuff</td> 
     </tr> 
    </tbody> 
    </table> 

    <p>Second paragraph</p> 
</div> 

我想處理程序分配給#inbox DIV不是表的一部分(孩子)內的所有內容。我試圖做一些像

$('#inbox:not(:has(table))').click(function(){ 
    alert('hello inbox'); 
}); 

但是,這並不工作,所以我如何反轉#inbox中的表的選擇?

回答

1

嘗試..

$('#inbox table').siblings().click(function(){ 
    alert('hello inbox'); 
}); 

$('#inbox *').not('table *,table').click(function(){ 
    alert('hello inbox'); 
}); 
1

由於單擊處理泡沫,你可以做幾件事情:

只是孩子,不表:

$("#inbox > *:not(:table)").click(function(e){ ... }); 

// or 

$("#inbox").children(':not(table)').click(function(e){ ... }): 

2.單個事件:

$("#inbox").click(function(e){ 
    // Not a descendant of table or this div 
    if($(e.target).closest('table').length === 0 && e.target !== this){ 
    // Do something 
    } 
}); 
0

所以我的選擇是:「一切#inbox,但不是table或任何table的後代:

$('#inbox *:not(table,table *)').bind('click', function(){ 
    alert($(this).text()) 
});