未捕获的ReferenceError:赋值中的左侧无效。使用Javascript

时间:2017-02-14 11:13:56

标签: javascript html

我想用另一个字符串替换字符串(original)中的字符 我在运行调试器时遇到错误 我不明白语法有什么问题。

<!DOCTYPE>
<html>

<head>
    <title>HI there</title>
    <meta lang="english">
</head>

<body>
    <div>
        Enter the original string<input id="original" value="" type="text"> <br> 
        Enter the replacing string<input id="replacing" value="" type="text"><br> 
        Enter the location to be replaced<input id="tobereplaced" value="" type="text"><br>
    </div>
    <br>
    <button type="submit" onclick="replace()">Submit</button>
    <br> Here you go the replaced string is:

    <script>
        function replace() {
            var original = document.getElementById("original").value;
            var replacing = document.getElementById("replacing").value;
            var tobereplaced = document.getElementById("tobereplaced").value;
            var replaced = "";
            var originalLength = original.length;
            var tobereplacedLength = tobereplaced.length;
            var k = 0;
            for (var i = 0; i < originalLength; i++) {
                replaced.charAt(k) = original.charAt(i)
                if (original.charAt(i) == replacing.charAt(0)) {
                    replaced = replaced + tobereplaced;
                    k = k + tobereplacedLength;
                    i++;
                }
                k++;
            }
            document.getElementById("replaced").innerHTML = replaced;

        }
    </script>
    <h1 id="replaced"></h1>
</body>

</html>

2 个答案:

答案 0 :(得分:1)

你试图在第28行更改空字符串的字符[replacement.charAt(k)= original.charAt(i)]这就是问题所在。 此外,代码中还有一些不需要的增量。请找到下面更正的

我已经更新了以下代码//注释代码并添加了正确的代码。它的工作

        // var k = 0; //Commented
        //  debugger; //Commented
        for (var i = 0; i < originalLength; i++) {

            if (original.charAt(i) == replacing.charAt(0)) {
                replaced = replaced + tobereplaced;
                // k = k + tobereplacedLength; //Commented
                // i++; //Commented
            } else{
            replaced = replaced + original.charAt(i);
            }
            // k++; //Commented
        }

答案 1 :(得分:0)

这是解决问题的更简单方法。它利用了.split()&amp; .join()函数而不是使用for循环。

&#13;
&#13;
function replace() {
  // set original string
  var original = document.getElementById("original").value, // << use commas so you don't have to keep typing var
    // set replacing string
    replacing = document.getElementById("replacing").value,
    // initialize newval
    newValue,
    // set replace position - this could also be called index
    replacePosition = document.getElementById("tobereplaced").value; // << end variable declarations with semicolon

  // split original string into array of characters
  var splitOriginal = original.split("");
  // use replacePosition as index value of character to replace 
  // & replace that character with replacing value
  splitOriginal[replacePosition] = replacing;
  // join array to form new string value
  newValue = splitOriginal.join("");

  document.getElementById("replaced").innerHTML = newValue;

}
&#13;
<html>

<head>
  <title>HI there</title>
  <meta lang="english">
</head>

<body>
  <div>
    Enter the original string
    <input id="original" value="" type="text">
    <br>Enter the replacing string
    <input id="replacing" value="" type="text">
    <br>Enter the location to be replaced
    <input id="tobereplaced" value="" type="text">
    <br>
  </div>
  <br>
  <button type="submit" onclick="replace()">Submit</button>
  <br>Here you go the replaced string is:


  <h1 id="replaced"></h1>
</body>

</html>
&#13;
&#13;
&#13;