[英]How to count number of <tr> contained in <tbody> using jQuery as well as javascript?
[英]Count the total number of rows in this tbody
我一直在嘗試計算嵌套tbody中tr的總數,但我無法獲得總數。 我試圖使用下面的jQuery代碼來獲取它,但是它返回的數字很大,例如44行,正如我期望的那樣,它返回7行。
關於我要去哪里,你能建議我嗎?
var count = $("#rbe_viewTab0 table tbody tr td table tbody tr").length;
alert(count);
<div id="rbe_viewTab0" class="nothing" style="display:block;margin-top:0px" isloaded="Y">
<table class="wide" cellspacing="0" cellpadding="10" border="0">
<tbody>
<tr>
<td>
<table class="wide" cellspacing="0" cellpadding="10" border="0">
<tbody>
<tr></tr>
<tr>
<td id="xyz">Row 2</td>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
我期望它返回的總行數為7行,第2行的索引為“ 1”。
嘗試
var count = $("#rbe_viewTab0 > table > tbody > tr > td > table > tbody > tr").length; alert(count);
我認為您的選擇器可能有誤。 這是一個片段。
var count = $("#rbe_viewTab0 > tbody > tr > td > table.wide > tbody > tr").length; var index = $("#rbe_viewTab0 > tbody > tr > td > table.wide > tbody > tr > td#xyz").parent().index(); alert(count); alert(index);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <table id="rbe_viewTab0"> <tr> <td> <table class="wide"> <tr> <td>Row 1</td> </tr> <tr> <td id="xyz">Row 2</td> </tr> <tr> <td>Row 3</td> </tr> <tr> <td>Row 4</td> </tr> <tr> <td>Row 5</td> </tr> <tr> <td>Row 6</td> </tr> </table> </td> </tr> </table>
編輯這是索引的選擇器。 還更新了我的代碼段。
var index = $("#rbe_viewTab0 > tbody > tr > td > table.wide > tbody > tr > td#xyz").parent().index();
$('tbody tbody tr').length
可以。
例如http://jsbin.com/bacosijina/1/edit?html,輸出
編輯:現在,您已經更新了問題: $('table table tr').length
就可以了。 即表包含的表中的行
編輯編輯:好的,問題中的標記再次更改。 也許您想使用$('#rbe_viewTab0 tbody tbody tr').length
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.