javascript - 如何使用 JQuery 查找 HTML 中的文本?

标签 javascript jquery

有没有办法执行一些 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/

相关文章:

triggers - 单击特定顺序后触发事件

javascript - jQuery clone() 函数弄乱了原始元素的属性

javascript - jQuery文件上传个人进展

javascript - 字符串操作,在数字之前插入一个字符串

javascript - 请求 Headers.append 不工作

javascript函数根据移动浏览器隐藏和显示超链接?

javascript - javascript中对象的数组数组

javascript - div中的进度条

javascript - 我可以在 AJAX post 请求中进行 mysql 查询吗

jquery 在Blur 上检查数字