jquery檢查一個(gè)表格有多少行的方法:1、使用“$("table").find("tr")”語(yǔ)句獲取到table表格中的tr對(duì)象;2、利用length屬性計(jì)算tr對(duì)象的長(zhǎng)度,即可獲取表格的行數(shù),語(yǔ)法“tr對(duì)象.length”。
本教程操作環(huán)境:windows7系統(tǒng)、jquery1.10.2版本、Dell G3電腦。
在表格中,一個(gè)tr標(biāo)簽對(duì)就表示一行數(shù)據(jù)。想要使用jquery檢查一個(gè)表格有多少行,就需要檢查一個(gè)表格有多少個(gè)tr標(biāo)簽對(duì)。
實(shí)現(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>獲取表格的行數(shù)</button> </body> </html>
相關(guān)教程推薦:jQuery視頻教程