Select any TR where a TD's text contains foo
I'm pretty sure this can be done without going into a function but I want to grab any TR where there is a <td>Mail ...</td>
or <td>Foo ... </td>
as examples.
I keep thinking it should look like
$开发者_运维问答('tr[./text()^="foo"]')
but chrome doesn't like it.
or $('tr[./td/text()^="Mail"]')
but no luck
can this be done with a simple selector?
I'm quite surprised no one came up with this, maybe my question was too vague.
$('tr:has(td:contains("Leather"))').hide()
$('tr:has(td:contains("Mail"))').hide()
$('tr:has(td:contains("Cloth"))').hide()
$('tr:has(td:contains("Bow"))').hide()
$('tr:has(td:contains("Gun"))').hide()
Going to make it a bookmarklet for searching for armor and weapons for a dk or pally
One possible solution:
$('td').filter(function() {
return $(this).text().indexOf('Foo') === 0;
}).closest('tr');
You have to use .filter()
to work on the element's text (you can't do this with a selector, at least not the way you want to) and .closest()
should be self explaining.
Edit: But this could select a <tr>
elements multiple times. I actually don't know how jQuery handles this. Another solution would be to use two nested filter
s (on tr
and on td
but somehow this feels inefficient).
Edit2: From version 1.4.4 on, jQuery seems to be smart enough to deal with multiple occurances of the same element (i.e. it works like a set). DEMO here (if you change to version 1.4.2, you'll see that the first row gets selected twice).
There is "contains:"
TD:contains("Mail")
But that just looks for Mail anywhere in the element (there is no 'starts with').
精彩评论