I want to use the jQuery contains selector only for a table with a unique id. I tried it like this but doesnt work.

var x = "tree";
$( "#TableID.tr:contains('" + x + "')").css( "background-color", "red" );

The code should change the backgroundcolor of every row which contains x in the tr tag in a table with the id TableID.

Can anyone help me with the correct syntax?

有帮助吗?

解决方案

you should use descendant selector for the table and the tr element

$( "#TableID tr:contains('" + x + "')").css( "background-color", "red" );

Your code looks for a table with id TableID with class tr and contains the given text some where within it

其他提示

You need to separte the .tr form the table id and remove the dot as it is not a class element.

$( "#TableID tr:contains('" + x + "')").css( "background-color", "red" );

Living demo

Try this,

$( "tr:contains('" + x + "')").css( "background-color", "red" );

Demo link http://jsfiddle.net/dhana36/YT3CD/2/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top