javascript刪除table表格的方法:首先使用“document.getElementById()”方法獲取table元素對(duì)象及其父元素對(duì)象;然后使用“父元素對(duì)象.removeChild(table元素對(duì)象)”語(yǔ)句來(lái)刪除表格即可。
本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。
javascript刪除table表格的方法:
-
獲取table表格的父元素對(duì)象和table對(duì)象
-
使用remove()方法刪除able表格對(duì)象
代碼示例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body id="body"> <table border="1" id="table"> <tr> <th>name</th> <th>job</th> </tr> <tr> <td>滅絕師太</td> <td>PHP中文網(wǎng)高級(jí)講師</td> </tr> <tr> <td>歐陽(yáng)克</td> <td>PHP中文網(wǎng)講師</td> </tr> <tr> <td>韋小寶</td> <td>PHP中文網(wǎng)初級(jí)講師</td> </tr> </table> <p id="demo">點(diǎn)擊按鈕,刪除table表格。</p> <button onclick="myFunction()">點(diǎn)擊按鈕</button> </body> <script> function myFunction() { var body = document.getElementById("body"); var table = document.getElementById("table"); body.removeChild(table); } </script> </html>
效果圖:
說(shuō)明:
remove()方法可用于刪除父節(jié)點(diǎn)上的所有元素,包括所有文本和子節(jié)點(diǎn)。
查找元素的方法:
1.根據(jù)id獲取元素
document.getElementById("id屬性的值");
2.根據(jù)標(biāo)簽名字獲取元素
document.getElementsByTagName("標(biāo)簽的名字");
3.根據(jù)name屬性的值獲取元素
document.getElementsByName("name屬性的值");
4.根據(jù)class屬性獲取元素
document.getElementsByClassName("類(lèi)樣式的名字");
5.根據(jù)css路徑獲取元素 (獲取一個(gè))
document.querySelector("css路徑");
6.根據(jù)css路徑獲取元素 (獲取一組)
document.querySelectorAll("css路徑");
【推薦學(xué)習(xí):javascript高級(jí)教程】