简体   繁体   中英

Matching tags and string in jQuery

Is it possible to match a string and it's enclosing tags in jQuery?

I need to match the following…

<p>Oops! We could not locate your form.</p>

and replace it or add a class to it - I'm not sure yet. I know how to match the content or the tags but I need to target the whole thing specifically to replace it.

Any ideas?

Thanks,

James

Use :contains :

$('p:contains("Oops! We could not locate your form.")')

Or filter :

$('p').filter(function() {
    return $(this).text() == 'Oops! We could not locate your form.';
});

The latter will be quicker, although neither are really ideal. If you need to find a common element in your HTML quite often, add a class or id to it.

Yeah! you can do, first you need to assign any class or id to that p tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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