<html><head><title></title>
<script language="javascript">
    var initTop=document.body.offsetHeight;
    function init()
    {
      document.write("<div id=layer1 style='position:absolute; width:196px; top:initTop;height:52px;z-index:1;background-color:red;'></div>")
    }
</script>
<style type="text/css"></style>
</head><body onload=init()>
</body></html>
我是这么添加,可是结果不对,请问有什么方法可以在脚本中添加标签吗?

解决方案 »

  1.   

    这样创建是可以的document.body.offsetHeight,这个错了,没这个属性的,去掉你就看到你创建的了 
      

  2.   

    <body onload='init()'><script language="javascript"> 
        
        function init() 
        { 
         var initTop=document.body.offsetHeight; 
          document.write(" <div id=layer1 style='position:absolute; width:196px; top:"+initTop+";height:52px;z-index:1;background-color:red;'> 123123</div>") 
        } 
    </script>
    </body>
      

  3.   


    <script language="javascript"> 
        
        function init() 
        { 
         var initTop=document.body.offsetHeight; 
          document.write(" <div id=layer1 style='position:absolute; width:196px; top:"+initTop+";height:52px;z-index:1;background-color:red;'> 123123</div>") 
        } 
    </script>
      

  4.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
      <META NAME="Generator" CONTENT="EditPlus">
      <META NAME="Author" CONTENT="">
      <META NAME="Keywords" CONTENT="">
      <META NAME="Description" CONTENT="">
     </HEAD> <BODY>
      <SCRIPT LANGUAGE="JavaScript">
      <!--
    var div=document.createElement("DIV");
    div.id="div1";
    div.style.background="yellow";
    div.style.width="120px";
    div.style.height="200px";
    div.innerText="哈哈哈哈哈";
    document.body.appendChild(div);
      //-->
      </SCRIPT>
     </BODY>
    </HTML>
      

  5.   


    <html> 
    <head> 
    <title> </title> 
    <script language="javascript">
        function init() 
        { 
          var initTop=document.body.offsetHeight;alert(initTop)
          document.write("<div id=layer1 style='position:absolute;width:196px;top:"+initTop+"px;height:52px;z-index:1;background-color:red;'></div>")
        } 
    </script> 
    <style type="text/css"> </style> 
    </head> <body onload=init()> 
    </body>
      

  6.   

    Web 开发常用手册JScript语言参考.rar
    http://download.csdn.net/source/308916DHTML参考手册.rar
    http://download.csdn.net/source/308913样式表中文手册.chm
    http://download.csdn.net/source/304124
      

  7.   


    initTop作为变量来用的,不能直接跟在top:后面