开发者

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?

: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>

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")')

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜