JavaScript 是否可以建立一个新的div,并且将内容写进去。可以的话,如何做,谢谢所有过来说两句的人。如果能给个例子最好。谢谢啦!

解决方案 »

  1.   


    <script language="JavaScript"> 
    window.onload=function () {
    var div = document.createElement("div");
    div.innerHTML = "xxx";
    document.body.appendChild(div);

    </script>
      

  2.   


         var div = document.createElement("div"); 
                div.setAttribute("id", "div1");
                div.setAttribute("innerHTML", "hello");
                document.body.appendChild(div);
      

  3.   

    动态时王道var innerIframe = document.createElement("IFRAME");
    innerIframe.setAttribute("name", "scrnews");
    innerIframe.setAttribute("marginWidth", "10");
    innerIframe.setAttribute("marginHeight", "10");
    innerIframe.setAttribute("frameBorder", "0");
    innerIframe.setAttribute("width", iframeWidth);
    innerIframe.setAttribute("height", "500");
    innerIframe.setAttribute("scrolling", "yes");
    innerIframe.setAttribute("allowTransparency", "false");
    innerIframe.setAttribute("src", srcs[i]);
    innerIframe.setAttribute("id", "iframe" + i);
      

  4.   

    document.body.appendChild(div); 这是在文档中添加子元素,可以把document换成其他你想要添加元素的地方,比若说odiv(层对象)等等;
    除了appendChild还有insertBefore
    要添加元素的对象.insertBefore(创建的对象,目标对象);即在目标前添加