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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:37:00  来源:igfitidea点击:

How do I select a span containing an exact text value, using jquery?

javascriptjquery

提问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');

.filterallows 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');