简体   繁体   English

如何使用带有ID选择器的JQuery Contains

[英]How to use JQuery Contains with id selector

I want to use the jQuery contains selector only for a table with a unique id. 我只想将jQuery包含选择器用于具有唯一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. 代码应更改ID为TableID的表的tr标记中包含x的每一行的背景色。

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. 您需要将.tr从表ID中分离出来,并删除点,因为它不是类元素。

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

Living demo 现场演示

you should use descendant selector for the table and the tr element 您应该对表和tr元素使用后代选择器

$( "#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 您的代码将查找ID为TableID且类为tr的表,并在其中包含给定文本的位置

Try this, 尝试这个,

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

Demo link http://jsfiddle.net/dhana36/YT3CD/2/ 演示链接http://jsfiddle.net/dhana36/YT3CD/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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