jquery清空表格除前兩行內(nèi)容的方法:1、使用“$("tr:first").empty();”語句選中表格第一行并清空該行內(nèi)容;2、使用“$("tr:nth-child(2)").empty();”語句選中表格第二行并清空該行內(nèi)容。
本教程操作環(huán)境:windows7系統(tǒng)、jquery1.10.2版本、Dell G3電腦。
jquery清空表格除前兩行內(nèi)容
實現(xiàn)方法:
-
利用
:first
選擇器選中表格第一行,并使用empty()清空內(nèi)容 -
利用
:nth-child(2)
選擇器選中表格第二行,并使用empty()清空內(nèi)容
說明:empty()方法可移除被選元素的所有子節(jié)點和內(nèi)容。
實現(xiàn)代碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function() { $("button").on("click", function() { $("tr:first").empty(); //清空表格第一行內(nèi)容 $("tr:nth-child(2)").empty(); //清空表格第二行內(nèi)容 }); }); </script> </head> <body class="ancestors"> <table border="1"> <tr> <th>商品</th> <th>價格</th> </tr> <tr> <td>T恤</td> <td>¥100</td> </tr> <tr> <td>牛仔褂</td> <td>¥250</td> </tr> <tr> <td>牛仔庫</td> <td>¥150</td> </tr> </table><br> <button>清空表格除前兩行內(nèi)容</button> </body> </html>
【推薦學習:jQuery視頻教程、web前端視頻】