且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery查找并替换字符串

更新时间:2023-02-21 15:33:09

你可以这样做:

$("span, p").each(function() {
    var text = $(this).text();
    text = text.replace("lollypops", "marshmellows");
    $(this).text(text);
});

标记所有标签会更好需要使用合适的类名检查的文本。

It will be better to mark all tags with text that needs to be examined with a suitable class name.

此外,这可能存在性能问题。一般来说,jQuery或javascript并不适合这种操作你***做服务器端。

Also, this may have performance issues. jQuery or javascript in general aren't really suitable for this kind of operations. You are better off doing it server side.