胖在一方

出得厅堂入得厨房的胖子

导航

Javascript(十四) Table

Posted on 2008-03-12 11:18  胖在一方  阅读(727)  评论(0)    收藏  举报

为了协助表格的建立,HTML DOM 给<table> <tbody> <tr>等元素都添加了方法
  
  ===========================
  <table/>添加了一下内容
  ===========================
  caption   指向<caption>元素(如果存在)
  tBodies   <tbody/>元素集合
  tFoot   <tfoot/>元素集合(如果存在)
  tHead   <thead/>元素集合(如果存在)
  rows   表中所有行的集合
  createTHead() 创建<thead/>元素,并将他放入表格中
  createTFoot() 创建<tfoot/>元素,并将他放入表格中
  createCaption() 创建<caption/>元素,并将他放入表格中
  deleteTHead()
  deleteTFoot()
  deleteCaption()
  deleteRow(position)  删除指定位置上的行
  insertRow(position)  在rows集合中的指定位置插入一个新行
  
  ===========================
  <tbody/>添加了一下内容
  ===========================
  rows   <tbody/>中所有行的集合
  deleteRow(poistion) 删除指定位置上的行
  insertRow(poistion) 在rows集合中的指定位置插入一个新行
    
  ===========================
  <tr/>添加了一下内容
  ===========================
  cells   <tr/>元素中所有单元格的集合
  deleteCell(position) 删除指定位置上的单元格
  insertCell(position) 在cells集合的给定位置上插入一个新的单元格.

function TableDemo1()
    {
        
var table =document.createElement("table");
        table.border
=1;
        table.width
=500;
        table.borderColor
="red";
     
     
        
var iRow=0;
        
var iCell=0;
        
        
//生成10行10列的表格
        for (iRow = 0;iRow < 10;iRow ++)
        {
           table.insertRow(iRow);
           
for(iCell = 0; iCell < 10 ; iCell++)
           {
                table.rows(iRow).insertCell(iCell);
                table.rows(iRow).cells(iCell).appendChild(document.createTextNode(iRow 
+ " " + iCell));
           }
        }
        
//添加表格    
        document.body.appendChild(table);
        table
=null;
    }

function TableDemo2()
    {
        
var table=document.createElement("table");
        table.border
=1;
        table.width
=500;
        table.borderColor
="red";
        
        
var iRow=0;
        
var iCell=0;
        
var row,cell;
        
        
for (iRow=0;iRow<10;iRow++)
        {
            row
=table.insertRow(iRow);
            
for (iCell=0; iCell<10; iCell ++)
            {
                cell
=row.insertCell(iCell);
                cell.appendChild(document.createTextNode(iRow 
+ " " + iCell));
                cell
=null;
            }
            row
=null;
        }
        document.body.appendChild(table);
        table
=null;
    }