最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery selector - Match content of elements - Stack Overflow

programmeradmin2浏览0评论

Is there any way - any jQuery selector (i did found none on / ), 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 pare their content in the cycle, but I want to be sure, there's no easier way.

Is there any way - any jQuery selector (i did found none on http://api.jquery./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 pare their content in the cycle, but I want to be sure, there's no easier way.

Share Improve this question asked Jan 12, 2011 at 20:05 Radek SimkoRadek Simko 16.1k18 gold badges72 silver badges110 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

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)

  1. 暂无评论