简体   繁体   English

jQuery选择器 - 匹配元素的内容

[英]jQuery selector - Match content of elements

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? 有什么办法 - 任何jQuery选择器(我在http://api.jquery.com/category/selectors/上没有找到),可以用作完全匹配吗?

:contains() is almost what i need, but not exactly. :contains()几乎是我需要的,但不完全是。 ":contains" searches in each element regex like this: “:contains”在每个元素正则表达式中搜索如下:

.*<query>.*

Which means, if i need to find link, which looks exactly like this: 这意味着,如果我需要找到链接,看起来完全像这样:

<a href="#">Baxter</a>

And use this: 并使用这个:

$("a:contains('Baxter')")

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];
};

You can then use this as $('a:contentIs("Baxter")') 然后你可以用它作为$('a:contentIs("Baxter")')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM