jQuery - 通过内部文本查找标签

发布于 2024-11-03 00:29:02 字数 287 浏览 1 评论 0原文

我想知道是否可以通过其内部文本找到标签(或任何元素,实际上)。例如:

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

我想通过文本SuperSweetCheckbox找到它。

我知道这似乎有点违反直觉,但由于我正在开发的应用程序的性质,这似乎是必要的。我意识到我可以迭代每个标签,但如果可能的话,我宁愿避免该选项。

如果有人可以提供一些帮助,我将不胜感激。

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>

And I want to find it by the text 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南巷近海 2024-11-10 00:29:02

使用选择器 :contains()

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

匹配文本可以直接出现在所选元素内、该元素的任何子元素或其组合中。与属性值选择器一样,:contains() 括号内的文本可以写为裸词或用引号括起来。文本必须具有匹配的大小写才能被选择。

use the selector :contains()

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

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.

迟到的我 2024-11-10 00:29:02

也许

$('label[value|="SuperSweetCheckbox"]')

根据:

http://api.jquery.com/attribute-contains-prefix-选择器/

$("label:contains('SuperSweet')")

根据

http://api.jquery.com/contains-selector/< /a>

Maybe

$('label[value|="SuperSweetCheckbox"]')

according to:

http://api.jquery.com/attribute-contains-prefix-selector/

or

$("label:contains('SuperSweet')")

according to

http://api.jquery.com/contains-selector/

云柯 2024-11-10 00:29:02

这是建立在卡斯帕的答案的基础上的,但太大了,无法发表评论。

我认为这对其他人可能有用。

我使用了Caspar的解决方案,但发现空字符串 "" 被认为存在于任何字符串中,请参阅:

https ://stackoverflow.com/a/18399216/1063287

在我的情况下,SuperSweetCheckbox 是一个动态值,有时是一个空字符串,在这种情况下,我不希望发生任何匹配逻辑。

使用 match() 创建伪函数似乎可行,请参阅:

https://stackoverflow.com/a /18462522/1063287

您还可以使用 filter(),请参阅:

https://stackoverflow.com/a/15364327/1063287 )。

下面是三个变体的示例 - :contains()filter() 和自定义函数:

jsFiddle

jsFiddle 链接

jQuery

var myString = "Test";

//var myString = "";

// 01.  :contains() - matches empty string as well
$("ul li > label:contains(" + myString + ")").closest("li").addClass("matched").siblings("li").addClass("notmatched");

// 02.  filter()
$("ul li > label").filter(function() {
    return $(this).text() === myString;
}).closest("li").addClass("matched").siblings("li").addClass("notmatched");

// 03.  custom function
$.expr[':'].textEquals = $.expr.createPseudo(function(arg) {
    return function( elem ) {
        return $(elem).text().match("^" + arg + "$");
    };
});

$("ul li > label:textEquals(" + myString + ")").closest("li").addClass("matched").siblings("li").addClass("notmatched");

HTML

<ul>
<li>
<label>Test</label>
</li>
<li>Oh Nu</li>
</ul>

CSS

.matched {
background: yellow
}

.notmatched {
background: aqua;
}

This builds on Caspar's answer, but was too big for comments.

I thought it could be useful for others.

I used Caspar's solution but found that an empty string "" is considered to exist in any string, see:

https://stackoverflow.com/a/18399216/1063287.

In my situation SuperSweetCheckbox is a dynamic value and sometimes an empty string, in which case I don't want any matching logic to occur.

Creating a pseudo function using match() seems to work, see:

https://stackoverflow.com/a/18462522/1063287.

You can also use filter(), see:

https://stackoverflow.com/a/15364327/1063287).

Below is an example of the three variants - :contains(), filter() and a custom function:

jsFiddle

jsFiddle link

jQuery

var myString = "Test";

//var myString = "";

// 01.  :contains() - matches empty string as well
$("ul li > label:contains(" + myString + ")").closest("li").addClass("matched").siblings("li").addClass("notmatched");

// 02.  filter()
$("ul li > label").filter(function() {
    return $(this).text() === myString;
}).closest("li").addClass("matched").siblings("li").addClass("notmatched");

// 03.  custom function
$.expr[':'].textEquals = $.expr.createPseudo(function(arg) {
    return function( elem ) {
        return $(elem).text().match("^" + arg + "$");
    };
});

$("ul li > label:textEquals(" + myString + ")").closest("li").addClass("matched").siblings("li").addClass("notmatched");

HTML

<ul>
<li>
<label>Test</label>
</li>
<li>Oh Nu</li>
</ul>

CSS

.matched {
background: yellow
}

.notmatched {
background: aqua;
}
跨年 2024-11-10 00:29:02

我认为 :contains() 选择器就是您正在寻找的。在这里检查样品> http://api.jquery.com/contains-selector/

I think the :contains() selector is what you are looking for. Check samples here > http://api.jquery.com/contains-selector/

寄居人 2024-11-10 00:29:02

上面指定的所有内容都是非常独特的,但也请尝试一下,如果它不起作用,请告诉我。

VAR $YesitHas=$("lable").innerText("SuperSweetCheckBox");

All the Above specified is very exclusive, but try out this too, if its not working let me know.

VAR $YesitHas=$("lable").innerText("SuperSweetCheckBox");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文