为什么getElementsByTagName不能在这个逻辑中工作?

时间:2014-12-09 01:02:22

标签: javascript html-table getelementsbytagname

只是测试一下,试图将一个表插入body标签,但我只能用getElementById来做。我也尝试在底部插入脚本代码,但这也不起作用。

<html>

<head>
        <title>Exam Test Trial</title>
        <script>
        var table;
            var books = {1234: "Programming for Dummies", 5667: "Ethical Hacking", 88889: "Networks", 10000: "Firmware Code"};

                function createTable(){
                table = "<table border= '1'>";

                table += "<tr>";

                for(var isbn in books){
                    table += "<th>";
                    table += isbn;
                    table += "</th>";
                }

                table += "</tr>";
                table += "</table>";


                //var x = document.getElementById("exam").innerHTML = table;

                var y;
            }


        </script>
</head>

<body id="exam">
    <script>
        createTable();
        var x = document.getElementsByTagName("body").innerHTML = table;
    </script>
</body>

1 个答案:

答案 0 :(得分:2)

getElementsByTagName("body")返回类似数组的结构。您的意思是getElementsByTagName("body")[0],也许是?