jquery檢查一個表格有多少行的方法:1、使用“$("table").find("tr")”語句獲取到table表格中的tr對象;2、利用length屬性計算tr對象的長度,即可獲取表格的行數,語法“tr對象.length”。
本教程操作環(huán)境:windows7系統(tǒng)、jquery1.10.2版本、Dell G3電腦。
在表格中,一個tr標簽對就表示一行數據。想要使用jquery檢查一個表格有多少行,就需要檢查一個表格有多少個tr標簽對。
實現(xiàn)代碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script src="js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("button").click(function() { var num=$("table").find("tr").length; alert(num); }); }); </script> </head> <body> <table border="1"> <caption>人物信息</caption> <tr> <th>姓名</th> <th>年齡</th> </tr> <tr> <td>Peter</td> <td>20</td> </tr> <tr> <td>Lois</td> <td>20</td> </tr> </table><br /> <button>獲取表格的行數</button> </body> </html>
相關教程推薦:jQuery視頻教程