日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DOM操作表格

發(fā)布時間:2025/7/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DOM操作表格 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前面的話

  表格table元素是HTML中最復(fù)雜的結(jié)構(gòu)之一。要想創(chuàng)建表格,一般都必須涉及表示表格行、單元格、表頭等方面的標(biāo)簽。由于涉及的標(biāo)簽多,因而使用核心DOM方法創(chuàng)建和修改表格往往都免不了要編寫大量的代碼。本文將詳細(xì)介紹DOM操作表格的屬性和方法

?

需求

  要通過DOM實(shí)現(xiàn)下列格式的表格結(jié)構(gòu)

<table border = "1" width = "100%"><tbody><tr><td>Cell 1,1</td><td>Cell 2,1</td></tr><tr><td>Cell 1,2</td><td>Cell 2,2</td></tr> </tbody> </table>

?

DOMcore

  如果通過DOMcore方法,則方法如下

//創(chuàng)建表格 var table = document.createElement("table"); table.border = "1"; table.width = "100%";//創(chuàng)建tbody var tbody = document.createElement("tbody"); table.appendChild(tbody);//創(chuàng)建第一行 var row1 = document.createElement("tr"); tbody.appendChild(row1); var cell1_1 = document.createElement("td"); cell1_1.appendChild(document.createTextNode("Cell 1,1")); row1.appendChild(cell1_1); var cell2_1 = document.createElement("td"); cell2_1.appendChild(document.createTextNode("Cell 2,1")); row1.appendChild(cell2_1);//創(chuàng)建第二行 var row2 = document.createElement("tr"); tbody.appendChild(row2); var cell1_2 = document.createElement("td"); cell1_2.appendChild(document.createTextNode("Cell 1,2")); row2.appendChild(cell1_2); var cell2_2 = document.createElement("td"); cell2_2.appendChild(document.createTextNode("Cell 2,2")); row2.appendChild(cell2_2);//將表格添加到文檔主體中 document.body.appendChild(table);

?

屬性和方法

  顯然DOM代碼很長,為了方便構(gòu)建表格,HTML DOM為<table>、<tbody>、<tr>元素添加了屬性和方法。

【1】為<table>元素添加的屬性和方法

caption:保存著對<caption>元素的指針 tBodies:是一個<tbody>元素的HTMLCollection tFoot:保存著對<tfoot>元素的指針 tHead:保存著對<thead>元素的指針 createTHead():創(chuàng)建<thead>元素,將其放到表格中,返回引用 createTFoot():創(chuàng)建<tfoot>元素,將其放到表格中,返回引用 createCaption():創(chuàng)建<caption>元素,將其放到表格中,返回引用 deleteTHead():刪除<thead>元素 deleteTFoot():刪除<tfoot>元素 deleteCaption():刪除<caption>元素

?【2】為<tbody>元素添加的屬性和方法

rows:保存著<tbody>元素中行的HTMLCollection deleteRow(pos):刪除指定位置的行 insertRow(pos):向rows集合中的指定位置插入一行,返回對新插入行的引用

【3】為<tr>元素添加的屬性和方法

cells:保存著<tr>元素中單元格的HTMLCollection deleteCell(pos):刪除指定位置的單元格 insertCell(pos):向cells集合中的指定位置插入一個單元格,返回對新插入單元格的引用

?

代碼重寫

//創(chuàng)建表格 var table = document.createElement("table"); table.border = "1"; table.width = "100%";//創(chuàng)建tbody var tbody = document.createElement("tbody"); table.appendChild(tbody);//創(chuàng)建第一行 tbody.insertRow(0); tbody.rows[0].insertCell(0); tbody.rows[0].cells[0].appendChild(document.createTextNode("Cell 1,1")); tbody.rows[0].insertCell(1); tbody.rows[0].cells[1].appendChild(document.createTextNode("Cell 2,1"));//創(chuàng)建第二行 tbody.insertRow(1); tbody.rows[1].insertCell(0); tbody.rows[1].cells[0].appendChild(document.createTextNode("Cell 1,2")); tbody.rows[1].insertCell(1); tbody.rows[1].cells[1].appendChild(document.createTextNode("Cell 2,2"));//將表格添加到文檔主體中 document.body.appendChild(table);

?

效果展示

<script> //創(chuàng)建表格 var table = document.createElement("table"); table.border = "1"; table.width = "100%";//創(chuàng)建tbody var tbody = document.createElement("tbody"); table.appendChild(tbody);//創(chuàng)建第一行 tbody.insertRow(0); tbody.rows[0].insertCell(0); tbody.rows[0].cells[0].appendChild(document.createTextNode("Cell 1,1")); tbody.rows[0].insertCell(1); tbody.rows[0].cells[1].appendChild(document.createTextNode("Cell 2,1"));//創(chuàng)建第二行 tbody.insertRow(1); tbody.rows[1].insertCell(0); tbody.rows[1].cells[0].appendChild(document.createTextNode("Cell 1,2")); tbody.rows[1].insertCell(1); tbody.rows[1].cells[1].appendChild(document.createTextNode("Cell 2,2"));//將表格添加到文檔主體中 document.body.appendChild(table); </script>

轉(zhuǎn)載于:https://www.cnblogs.com/xiaohuochai/p/4839792.html

總結(jié)

以上是生活随笔為你收集整理的DOM操作表格的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。