替换脚本只获取第一个实例,而不是全部

时间:2015-09-18 15:20:22

标签: jquery

正如标题所说。使用的代码不会替换公司名称的所有实例。该网站正在使用基础,如果我将脚本粘贴到不同的部分,则可以使用。

以下是代码:

$(function () {
    $("body").children().each(function () {
        $(this).html($(this).html().replace("Oblique Creation Inc.", "Obl<span style='color:red;'>i</span>que Creation Inc."));
    });
});

感谢您提前提供任何帮助。

1 个答案:

答案 0 :(得分:0)

当使用字符串作为该字符串的第一个参数only replaces the first occurrence时,Javascript替换函数。

相反,请使用正则表达式:

$(this).html().replace(/Oblique Creation Inc/g, "Obl<span style='color:red;'>i</span>que Creation Inc");