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

时间:2014-08-28 03:35:26

标签: javascript

我在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>

2 个答案:

答案 0 :(得分:1)

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

Uncaught SyntaxError: Unexpected token else

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

应该是:

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

答案 1 :(得分:-2)

首先创建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>