How to undo the context

I want to select elements, but not if one of their ancestors matches a particular selector.

For example, suppose I want to map all <a> nodes that are not children of a table.

I tried something like this:

 $("a", ":not(table *)"); 

but this causes my browser to crash.

This one also hangs in my browser:

 jQuery("a", ":not(table td)"); 

The requested page is quite large, with a lot of very large tables. So, I need something that works well. Any ideas?

+6
jquery css-selectors
source share
3 answers

Other answers make the situation too complicated.

 $('a').not('table a'); 

-or -

 $('a:not(table a)'); 

The second parameter in the jQuery function is the context for searching for the element, but when you select :not(table) , each element that is not a table that includes the descendants of the tables will be selected. Usually you want to use the context parameter when you have a specific document or item under which you want to search.

+6
source share
 $('a').not(function() { return $(this).closest('table').length > 0; }); 

or more likely to be faster if they are always direct children of the <td>

 $('a').not(function() { return $(this).parent('td').length > 0; }); 
0
source share

Using:

 $("a").not("td a") 

Check this script .

0
source share

All Articles