Find and Change Class
Suppose we have table like that :
<table id="tbl">
<tr><td>1</td>开发者_开发问答;<td>abc</td></tr>
<tr><td>2</td><td>def</td></tr>
<tr><td>3</td><td>abc</td></tr>
<tr><td>4</td><td>def</td></tr>
</table>
How can I change the row's class to below class when a row contains abc?
.hightlight
{
background-color:yellow;
color:blue;
}
I have used this jquery command, but not working :
$('#tbl tr td:contains("abc")').attr('class','hightlight');
Code in fiddle works fine @ http://jsfiddle.net/Jayendra/nLYuW/
If you want whole row, you need to use parent
$('#tbl tr td:contains("abc")').parent().attr('class','hightlight');
Your code seems to be working as-is. You could try changing your .attr()
to addClass()
as an alternate method.
$('#tbl tr td:contains("abc")').addClass('hightlight');
Here's a working fiddle.
If you would like to add the CSS class to the entire row, then use the .parent()
selector.
$('#tbl tr td:contains("abc")').parent().addClass('hightlight');
you could also use this:
$("#tbl tr").filter(':has(td:contains("abc"))').addClass("highlight");
精彩评论