有没有办法执行一些 HTML 搜索,并且每当发现正则表达式出现时,将其包装在一个跨度中?
例如:
<p>The rain in Spain stays mainly on the plain</p>
$('p').match('/ain/gi').each(function() {
$(this).wrap('<span class="red"></span>');
});
最佳答案
我强烈建议采用以下更简单的方法:
// 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>');
});
关于javascript - 如何使用 JQuery 查找 HTML 中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25855236/