jQuery选择器 - 匹配元素的内容 [英] jQuery selector - Match content of elements
问题描述
有没有办法 - 任何jQuery选择器(我确实没有在 http://api.jquery上找到) .com / category / selectors / ),可以用作完全匹配吗?
Is there any way - any jQuery selector (i did found none on http://api.jquery.com/category/selectors/ ), which can be used as exact match?
:contains()几乎是我需要的,但不完全是。
:包含每个元素正则表达式中的搜索:
:contains() is almost what i need, but not exactly. ":contains" searches in each element regex like this:
.*<query>.*
这意味着,如果我需要找到链接,看起来完全像这样:
Which means, if i need to find link, which looks exactly like this:
<a href="#">Baxter</a>
并使用此:
$("a:contains('Baxter')")
It也匹配这个,我不想匹配:
It matches also this, which i don't want to match:
<a href="#">I'm Peter Baxter, how are you?</a>
我知道,我可以采取所有元素并在周期中比较它们的内容,但我想要可以肯定的是,没有更简单的方法。
I know, that I can just take all elements and compare their content in the cycle, but I want to be sure, there's no easier way.
推荐答案
不,但添加插件会很简单:
No, but it would be trivial to add as a plugin:
$.expr[':'].contentIs = function(el, idx, meta) {
return $(el).text() === meta[3];
};
然后您可以将其用作 $('a:contentIs(Baxter) )')
这篇关于jQuery选择器 - 匹配元素的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!