如何使用JQuery在HTML中查找文本?
有没有办法对某些HTML进行搜索,每当发现正则表达式时,将其包装在一个范围内?
例如:
The rain in Spain stays mainly on the plain
$('p').match('/ain/gi').each(function() { $(this).wrap(''); });
我强烈建议采用以下更简单的方法:
// 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, '$1'); });