سؤال

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