到目前为止,这是我的代码:
$("h1.intro:contains('|')").each(function() {
$(this).html($(this).html().replace('|','</span><br /><span>'))
});
这只适用一次,但它必须适用于所有这些“|”......
任何想法?
答案 0 :(得分:14)
添加/g
修饰符:
$("h1.intro:contains('|')").each(function() {
$(this).html($(this).html().replace(/\|/g, '</span><br /><span>'));
});
更多信息:
g修饰符用于执行a 全局匹配(找到所有匹配 而不是在第一场比赛后停止。)
答案 1 :(得分:2)
如果您使用的是jQuery 1.4,则可以使用.html(function))
签名更好地完成此操作:
$("h1.intro:contains('|')").each(function() {
$(this).html(function(idx, oldContent) {
return oldContent.replace(/\|/g, '</span><br /><span>');
});
});
这意味着您不必创建第二个jQuery实例,并且应该执行得更好。
答案 2 :(得分:0)
您可以在“|”
之后添加“/ g”,为正则表达式添加修饰符$("h1.intro:contains('|')").each(function() {
$(this).html($(this).html().replace("|/g",'</span><br /><span>'))
});