Jquery替换每个

use*_*846 2 javascript each jquery replace

我想用span标签替换每个链接中的每个"New".

这样对吗?

$("a:contains('New')").each(function () {
                 $(this).html($(this).html().replace("New", "<span class='new'>New</span>"));
        });
Run Code Online (Sandbox Code Playgroud)

Tat*_*nit 7

正则表达式:工作演示 http://jsfiddle.net/WujTJ/

g =/g修饰符确保所有出现的"替换"

i - /i使正则表达式匹配不区分大小写.

好读:如果你热衷于:http://www.regular-expressions.info/javascript.html

请试试这个:

希望这有帮助 :)

另请注意,您可能不需要检查它是否包含New,因为正则表达式会在需要时更改:

   // $("a:contains('New')").each(function () { // NOT sure if you need this
                     $(this).html($(this).html().replace(/New/g, "<span class='new'>New</span>"));
      //      });
Run Code Online (Sandbox Code Playgroud)