简体   繁体   中英

How to use JQuery Contains with id selector

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 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

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

Try this,

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM