jQuery - 通过内部文本查找标签 [英] jQuery - Find Label By The Inner Text

查看:1146
本文介绍了jQuery - 通过内部文本查找标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以通过内部文本找到一个标签(或任何元素,真的)。例如:

I was wondering if it was possible to find a label (or any element, really) by it's inner text. For example:

<label for="myCheckbox">SuperSweetCheckbox</label>

我想通过文本 SuperSweetCheckbox

我知道这似乎是一种反直觉,但由于我正在处理的应用程序的性质似乎是必要的。我意识到我可以迭代每一个标签,但如果可以的话我宁愿避免这个选择。

I know this seems kind of counter-intuitive, but due to the nature of the app I'm working on it seems to be necessary. I realize that I can iterate through each of the labels but I'd prefer to avoid that option if at all possible.

如果有人可以提供一些帮助,我会欣赏它。

If anyone could provide some assistance, I'd appreciate it.

推荐答案

使用选择器 :contains()

use the selector :contains()

 var element = $("label:contains('SuperSweetCheckbox')");

匹配的文本可以直接在所选元素中显示 元素的后代或其组合。与属性值选择器一样,括号内的:contains()的文本可以写成裸字或用引号括起来。文本必须具有匹配的大小写。

The matching text can appear directly within the selected element, in any of that element's descendants, or a combination thereof. As with attribute value selectors, text inside the parentheses of :contains() can be written as bare words or surrounded by quotation marks. The text must have matching case to be selected.

这篇关于jQuery - 通过内部文本查找标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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