2010-06-15 6 views
2

J'ai une partie de page comme celle-table inverti JQuery sélection

<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> 

Je veux affecter un gestionnaire tout dans le #inbox div qui ne fait pas partie (un enfant) de la table. J'ai essayé de faire quelque chose comme

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

Mais cela ne fonctionne pas, alors comment puis-je inverser la sélection de la table à l'intérieur du #inbox?

Répondre

1

essayer ..

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

ou

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

Depuis bouillonner des gestionnaires de clic, vous pouvez faire quelques petites choses:

1. Juste les enfants, pas la table :

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

// or 

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

2. Un événement unique:

$("#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

Donc, mon sélecteur est: « tout #inbox, mais pas un table ou un descendant d'un table:

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