Javascript 如何找到包含确切文本字符串的特定类的跨度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5490002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find a span with a specific class that contains an exact string of text?
提问by Ben McCormack
I'm trying to find the following node within a page:
我正在尝试在页面中找到以下节点:
<span class="dotted">Admin</span>
I've tried the following jQuery selectors, but neither seem to work in selecting the node:
我尝试了以下 jQuery 选择器,但似乎都无法选择节点:
$(".dotted span:contains('Admin')").css("color","red");
$("span:contains('Admin') .dotted").css("color","red");
What am I doing wrong?
我究竟做错了什么?
回答by John Hartsock
This selector should do what you want.
这个选择器应该做你想做的。
$("span.dotted:contains('Admin')")
Remember you can chain Tag names with IDs and classes. Example
请记住,您可以将标签名称与 ID 和类链接在一起。例子
$("span#yourID")
OR
或者
$("span.yourclass")
OR a mixture of the two
或两者的混合
$("span#yourID.yourClass")