我目前正在努力编写一个脚本,该脚本应该替换一些文本并通过 css 将其设为粗体。
但我的问题是,脚本没有循环。它只需要第一个文本。
谁能帮帮我?
这是 my fiddle .
这是我的代码
$(document).ready(function (i) {
$('.main:contains("*")').each(function () {
$(this).html($(this).html().replace('*', '<span class="highlight">'));
$(this).html($(this).html().replace('*', '</span>'));
});
});
最佳答案
您应该使用带有 g
标志的正则表达式。像这样的东西:
$('.main:contains("*")').html(function (_, html) {
return html.replace(/\*(\w+)\*/g, "<span class='highlight'>$1</span>");
});
关于jquery - 遍历文本并用 span 替换 *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29316341/