jQuery根据文本内容来选择元素 [英] jQuery to select elements based on the text inside them

查看:82
本文介绍了jQuery根据文本内容来选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何选择其中具有特定文本的标签,没有其他内容?例如,如果我有以下内容:

How to select a tag having a specific text inside it and nothing else? For example if I have the following:

<table>
    <tr>
       <td>Assets</td><td>Asset</td>
    </tr>
    <tr>
       <td>Play</td><td>Players</td><td>Plays</td>
    </tr>
</table>

有什么办法可以选择< td>资产& / td> ,没有别的。我尝试使用包含 ie $(table tr td:contains(Play))但它返回所有 td 在第二个 tr (应该)。而我只想要< td> Play< / td>

Is there any way that I may select the <td>Asset</td> and nothing else. I tried it with contains i.e. $("table tr td:contains(Play)") but it returns all the tds in the second tr (as it should). Whereas I just want <td>Play</td>.

有没有办法实现这一点,就像有一种方式可以根据其属性选择元素。有没有办法根据他们内部的文本来选择元素?

Is there any way to achieve this, like there's a way to select elements based on their attributes. Is there any way to select elements based on the text inside them?

推荐答案

如何:

var lookup = 'Asset';
$('td:contains('+ lookup +')').filter(function() {
    return $(this).text() === lookup;
});

演示

尝试购买之前

这篇关于jQuery根据文本内容来选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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