jquery在里面找到文本< td> [英] jquery find text inside <td>

查看:99
本文介绍了jquery在里面找到文本< td>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩一些选择器,而且我在一个单元格中选择Text时会撞墙。

I'm playing around with some selectors, and I'm hitting a wall with selecting Text inside a cell.

这是我正在做的一个简单的尝试。

Here's a simple attempt I'm making.

<table>
 <tr>
     <td>First Name</td>
     <td>*required</td>
  </tr>
</table> 

我想将该单元格的类更改为红色 - 如果字符串为* required 找到了。

I want to change the class for that cell to be "red" - if the string "*required" is found.

这是我对jquery的尝试:

Here's my attempt at the jquery:

$("td:contains('*required')").addClass("red");

它似乎导致所有单元格应用该类。
有什么更好的方法可以查找特定文本吗?

It's causing all cells to apply that class, it seems. Any better ways to look for specific text?

推荐答案

你有什么工作,您可以在这里测试,请记住任何父母 < td> 包含该文本,为了完全匹配,请执行以下操作:

What you have works, you can test it here, keep in mind that any parent <td> also contains that text though, to do an exact match do this:

$("td").filter(function() { return $.text([this]) == '*required'; })
       .addClass("red");

您可以在此处测试

这篇关于jquery在里面找到文本&lt; td&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆