Am在chrome调试器中加载时收到错误myFunction未定义

Am receiving error myFunction not defined when loading in chrome debugger

本文关键字:错误 myFunction 未定义 加载 chrome 调试器 Am      更新时间:2023-09-26

Am在chrome调试器中收到未定义myFunction的通知。非常需要帮助。非常感谢。

<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through a block of as long as i is less than 10.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
    var text = "";
    var i;
        if (math.random > .5) i === "Heads"}
            else { i === "tails"
    };
    while (i < 100) {
        text += "<br>This Is " + i;
        i++;
    }
    document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>

使用您提供的代码,您还应该在控制台中看到另一个错误:

Uncaught SyntaxError: Unexpected token else

if (math.random > .5) i === "Heads"}
    else { i === "tails"
  };

应为:

if (Math.random() > .5) {
     i = "Heads";
}
else { 
    i = "tails";
};

首先创建Javascript函数并调用它

<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
 var text = "";
      var i;
      if (math.random > .5) i === "Heads"}
        else { i === "tails"
      };
    while (i < 100) {
      text += "<br>This Is " + i;
      i++;
    }
    document.getElementById("demo").innerHTML = text;
}
</script>
<p>Click the button to loop through a block of as long as i is less than 10.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>