javascript 如何使用 jquery 选择包含精确文本值的跨度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9424509/
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 select a span containing an exact text value, using jquery?
提问by MedicineMan
Possible Duplicate:
Select element based on EXACT text contents
可能的重复:
根据 EXACT 文本内容选择元素
How do I select a span containing an exact text value, using jquery?
如何使用 jquery 选择包含精确文本值的跨度?
<div>
<span>find me</span>
<span>dont find me</span>
<span>xfind mex</span>
<span>_find me_</span>
</div>
回答by Brad Christie
$('span')
.filter(function(){ return $(this).text() == 'find me'; })
.css('color','red');
.filter
allows you to apply logic to the elements and return only those that match your test(s) back.
.filter
允许您将逻辑应用于元素并仅返回与您的测试匹配的元素。
In case you want a better integrated version, here's a :text()
selector:
如果你想要一个更好的集成版本,这里有一个:text()
选择器:
(function($){
$.expr[':'].text = function(obj, index, meta, stack){
return ($(obj).text() === meta[3])
};
})(jQuery);
$('span:text("find me")').css('color','red');