jQuery - 通过内部文本查找标签
我想知道是否可以通过其内部文本找到标签(或任何元素,实际上)。例如:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用选择器 :contains()
匹配文本可以直接出现在所选元素内、该元素的任何子元素或其组合中。与属性值选择器一样,
:contains()
括号内的文本可以写为裸词或用引号括起来。文本必须具有匹配的大小写才能被选择。use the selector :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.也许
根据:
http://api.jquery.com/attribute-contains-prefix-选择器/
或
根据
http://api.jquery.com/contains-selector/< /a>
Maybe
according to:
http://api.jquery.com/attribute-contains-prefix-selector/
or
according to
http://api.jquery.com/contains-selector/
这是建立在卡斯帕的答案的基础上的,但太大了,无法发表评论。
我认为这对其他人可能有用。
我使用了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
HTML
CSS
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
HTML
CSS
我认为
: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/上面指定的所有内容都是非常独特的,但也请尝试一下,如果它不起作用,请告诉我。
All the Above specified is very exclusive, but try out this too, if its not working let me know.