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

Unable To Display The Output In The Border Using Functions In Javascript

本文关键字:边框 显示 输出 函数 javascript      更新时间:2023-09-26
<!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 乘法表的样式。为什么我无法在边框中显示我的输出..!请问我需要解决方案!!

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>