日期:2014-05-16  浏览次数:20482 次

用JS and DOM生成表格
<script language="javascript">
     function start(){
       mybody = document.getElementsByTagName  ("body").item(0);
        mytable = document.createElement("TABLE");
        mytablebody = document.createElement("TBODY");
        for(j=0;j<2;j++){
           mycurrent_row = document.createElement("TR");
           for(i=0;i<2;i++){
               mycurrent_cell= document.createElement("TD");
              currenttext=document.createTextNode("cell is  row "+j+",  column "+i);
              mycurrent_cell.appendChild(currenttext);
              mycurrent_row.appendChild(mycurrent_cell);
           }
           mytablebody.appendChild(mycurrent_row);
       }
       mytable.appendChild(mytablebody);
       mybody.appendChild(mytable);
       mytable.setAttribute("border","2");
   }
  </script>
?原来mybody = document.getElementsByTagName? ("body").item(0);是mybody =???? document.getElementsByTagName? ("body");写的,报错“对象不支持属性或方法”后来发现getElementsByTagName()方法返回一个集合,所以改成代码中的写法通过。很不好找呀
1 楼 cherryQQ 2008-04-18  
只能在firefox浏览器中实现
2 楼 7thbyte 2008-04-18  
document.getElementsByTagName  ("body").item(0);

直接用document.body不就可以了么?