admin管理员组

文章数量:1415467

I would like to use jQuery to select all rows in a table that don't have a td containing certain text.

I can select the rows with this line:

var x = $('td:contains("text"):parent');  //muliple td's in each tr

How would I use the :not selector to invert the selection?

edit: I don't think the line of code above is really accurate. This is how I originally had the line:

var x = $('td:contains("text")).parent();  //muliple td's in each tr

When I tried to invert the selection, I get all the rows as they all happen to contain a td not containing the text.

I would like to use jQuery to select all rows in a table that don't have a td containing certain text.

I can select the rows with this line:

var x = $('td:contains("text"):parent');  //muliple td's in each tr

How would I use the :not selector to invert the selection?

edit: I don't think the line of code above is really accurate. This is how I originally had the line:

var x = $('td:contains("text")).parent();  //muliple td's in each tr

When I tried to invert the selection, I get all the rows as they all happen to contain a td not containing the text.

Share Improve this question edited May 22, 2013 at 17:54 user1634700 asked May 22, 2013 at 17:09 user1634700user1634700 1753 silver badges12 bronze badges 1
  • $('td:not(:contains("text")):parent');.. – Adil Shaikh Commented May 22, 2013 at 17:11
Add a ment  | 

2 Answers 2

Reset to default 5

Try this:

var $x = $('td:not(:contains("text")):parent');

FIDDLE DEMO

Case 1: Select all TR that contains text 'my text' in all TD's

I wouldn't rely too much on the pseudo. Try something like below using filters, (internally pseudo are going to do the same anyway)

$('tr').filter(function () {
   return $(this).find('td').filter(function () {
                return $(this).text().indexOf('myText') == -1;
          }).length;
}); //would return all tr without text 'myText'

DEMO: http://jsfiddle/dWuzA/

Case 2: Select all TR that contains text 'my text' in any TD's

@squint made an excellent point in ment

So incase if you want to select all TR that contains doesn't has a specific text in any of the TD's, then you can inverse the conditions.. See below,

DEMO: http://jsfiddle/dWuzA/1/

$(function () {
    $('tr').filter(function () {
        return !$(this).find('td').filter(function () {
            return $(this).text().indexOf('22') != -1;
        }).length;
    }).addClass('highlight');
});

本文标签: javascriptjquery select all rows not containing specific textStack Overflow