Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何替换循环中文本中的所有文本?_Javascript_Jquery_Replace - Fatal编程技术网

Javascript 如何替换循环中文本中的所有文本?

Javascript 如何替换循环中文本中的所有文本?,javascript,jquery,replace,Javascript,Jquery,Replace,对不起,我的英语不好= 我需要替换页面上的一些元素。这是我的密码: var text1 = $('body').html().replace(/text1/, 'text11'); var text2 = $('body').html().replace(/text2/, 'text22'); var text3 = $('body').html().replace(/text3/, 'text33'); array = [text1, text2, text3]; for (var

对不起,我的英语不好= 我需要替换页面上的一些元素。这是我的密码:

var text1 = $('body').html().replace(/text1/, 'text11');
var text2 = $('body').html().replace(/text2/, 'text22');
var text3 = $('body').html().replace(/text3/, 'text33');
array = [text1, text2, text3];
      for (var i = 0; i < array.length; i++) {
        $('body').html(array[i])
      };

但要仅替换第一个和第三个元素,如果删除第三个元素数组,则第一个和第二个元素数组将更改。请告诉我怎么做。谢谢大家!

这是因为在计算变量时,原始主体html保持不变。您需要将替换链接起来,或将以前的变量用于另一个替换:

var text1 = $('body').html().replace(/text1/, 'text11');
var text2 = text1.replace(/text2/, 'text22');
var text3 = text2.replace(/text3/, 'text33');
然后你就不需要任何迭代了

$('body').html(text3);

这是因为在计算变量时,原始主体html保持不变。您需要将替换链接起来,或将以前的变量用于另一个替换:

var text1 = $('body').html().replace(/text1/, 'text11');
var text2 = text1.replace(/text2/, 'text22');
var text3 = text2.replace(/text3/, 'text33');
然后你就不需要任何迭代了

$('body').html(text3);

链接通常指输入。替换…。替换…。替换…@JanDvorak。重读这篇文章。链接替换或使用前面的变量替换通常指输入。替换…,替换…,替换…@JanDvorak。重读这篇文章。链替换或使用以前的变量FWIW,RegEx.FWIW中不需要,RegEx中不需要。