DOM動作表格——HTML DOM

來源:互聯網
上載者:User

標籤:

html建立表格:

<table berder=‘1‘ width=‘300‘> <thead> <tr> <th>姓名</th> <th>性別</th> <th>年齡</th> </tr> </thead> <tbody> <tr> <th>tzr</th> <th>男</th> <th>24</th> </tr> </tr> </tbody> <tfoot> <tr> <th>結束</th> </tr> </tfoot> </table>

  而利用DOM建立方法為:

window.onload=function(){    var table=document.createElement(‘table‘);    table.width=300;    table.border=1;}

  利用appendChild()方法添加表格的thead、tr,th。如:

var thead=document.createElement(‘thead‘);table.appendChild(thead);

  可以看出,使用DOM建立表格比較累。

  下面看看如何使用DOM擷取表格式資料:擷取table第一個子節點caption的內容。

window.onload=function(){    var table=document.getElementByTagName(‘table‘)[0];    alert(table.children[0].innerHTML);}

  所以,我們常使用HTML DOM。

  table.caption.innerHTML;

  table.caption.innerHTML=‘人員表‘;

  table.thead;

  table.tfoot;

  table.tBodies;   //返回tbody集合

  table.rows;    //所有tr集合

  table.tBodies[0].rows;   //主體tbody的tr集合

  table.tBodies[0].rows[1].cells[1].innerHTML;   //返回主體第二行第二個儲存格的內容

  table.deleteCaption();

  table.deleteTHead();

  table.tBodies[0].deleteRow(0);    //刪除主體第一行

  table.tBodies[0].rows[1].deleteCell(1);   //刪除主體第二行第二個資料

  最後,我們可以簡單快捷地建立一個表格。

  table.createCaption().innerHTML=‘人員表‘;

  var thead=table.createTHead();

  var tr=thead.insertRow(0);

  tr.insertCell(0).innerHTML=‘資料1‘;

  tr.insertCell(1).innerHTML=‘資料2‘;

  tr.insertCell(2).innerHTML=‘資料3‘;

  PS:在建立表格時,<table>,<tbody>,<th>沒有特定的方法。

 

DOM動作表格——HTML DOM

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.