为什么我的查找和替换代码无法正常工作?

时间:2012-12-21 00:00:41

标签: javascript jquery

我需要找到和.replaceWith一些文本,例如:

  

absdef 替换为 111111

     

abcdefgiop 替换为 22222222

但是当我的文字看起来像是

  

abcdef,abcdefgi

.replaceWith对我来说工作很差,我得到这样的东西:

  

111111 ,111111giop

我使用此代码:

$("*").contents().each(function() {
    if(this.nodeType == 3)
        this.nodeValue = this.nodeValue.replace("absdef", "111111");
});
//
$("*").contents().each(function() {
    if(this.nodeType == 3)
        this.nodeValue = this.nodeValue.replace("abcdefgiop", "22222222");
});

1 个答案:

答案 0 :(得分:0)

撤销订单并连接它们。

$("*").contents().each(function() {
    if(this.nodeType == 3)
        this.nodeValue = this.nodeValue.replace("abcdefgiop", "22222222")
                                       .replace("abcdef", "111111");
});