如何在body标签后动态添加div到我的页面?

时间:2012-07-21 15:23:10

标签: javascript

我编写了以下代码,只是将它放在结束标记之前的div 我希望它在开头。

<script type="text/javascript" language="javascript">

        window.onload = function createDiv1() {
            var divTag1 = document.createElement("div");

            divTag1.id = "mid section";

            divTag1.setAttribute("align", "center");

            divTag1.className = "dynamicDivmid";

            divTag1.innerHTML = "pp";
            document.body.appendChild(divTag1);


        }

1 个答案:

答案 0 :(得分:7)

试试这样:

document.body.insertBefore(divTag1, document.body.firstChild)