无法在Javascript中使用函数显示边框中的输出

时间:2015-11-06 13:07:39

标签: javascript html5 css3

<!DOCTYPE html>
<html>
<head>
    <title>Table-2</title>
</head>
<body>
    <div onload="myFunction()"></div>
    <script>
        function myFunction() {
            var num = 2;
            for (var i = 1; i < 11; i++) {
                document.write(num + " * " + i + " = " + num * i);
                document.write('<br>');
            }
        }
    </script>
</body>
</html>

我试图使用函数设置我的2个乘法表的样式..为什么我无法在边框中显示我的输出..!我需要解决方案!!!

1 个答案:

答案 0 :(得分:0)

div未加载,这就是javascript未执行的原因。

但是,body确实已加载。使用此:

<!DOCTYPE html>
<html>
<head>
    <title>Table-2</title>
</head>
<body onload="myFunction()">
    <script>
        function myFunction() {
            var num = 2;
            for (var i = 1; i < 11; i++) {
                document.write(num + " * " + i + " = " + num * i);
                document.write('<br>');
            }
        }
    </script>
</body>
</html>