简体   繁体   English

在jQuery中匹配标签和字符串

[英]Matching tags and string in jQuery

Is it possible to match a string and it's enclosing tags in jQuery? 是否有可能匹配一个字符串和它在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 : 使用:contains

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

Or filter : 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. 如果您需要经常在HTML中查找公共元素,请为其添加classid

Yeah! 是啊! you can do, first you need to assign any class or id to that p tag. 你可以做,首先你需要为那个p标签分配任何类或id。

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

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