如何使用JQuery在HTML中查找文本? [英] How can I find text in HTML with JQuery?
本文介绍了如何使用JQuery在HTML中查找文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以搜索一些HTML,并且每当发现一个正则表达式时,都将其包装在一个span中?
Is there a way to perform a search some HTML and whenever it finds an occurrence of a regular expression, wrap it in a span?
例如:
<p>The rain in Spain stays mainly on the plain</p>
$('p').match('/ain/gi').each(function() {
$(this).wrap('<span class="red"></span>');
});
推荐答案
我强烈建议以下更简单的方法:
I'd strongly suggest the following, simpler, approach:
// selects the 'p' elements, and calls the 'html()' method:
$('p').html(function (i,h) {
// uses the anonymous function, i is the index of the current element
// from the collection, h is the found HTML of that element.
return h.replace(/(ain)/gi, '<span class="red">$1</span>');
});
这篇关于如何使用JQuery在HTML中查找文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文