2010-06-15 57 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()) 
});