1

我是 jquery 的新手,我想计算表中的现有元素,html 结构如下:

<table summary="">
    <tr>
       <td>
           <table id="myid" some more attributes> <-- i got that id and 
               <tbody>
                   <tr>foo</tr> <---counting
                   <tr>bar</tr> <---counting
               </tbody>
            </table>
         </td>
      </tr>
 </table>
HTML is apex gernerated.

我已经尝试过来自jQuery 的 jquery 语句:count of rows in a table an mores sats 但对我没有任何作用。我不工作的“解决方案”是

 $("table[id='myid'] > tbody >tr").length

或者

 $('table#myid >tbody:last >tr').length

我很感谢任何提示和提示

马里奥

4

7 回答 7

3
于 2012-08-30T13:48:43.143 回答
2

try this.

http://jsfiddle.net/L7Nea/1/

JS

$('#myid tbody tr').length); 
于 2012-08-30T13:49:14.977 回答
1

如果您的意思是在您的特定表格内

$('#myid tr').length // <-- this gets all tr inside table with id=myid

或者,如果您真的想遍历 dom 并更具体,以防该表内还有另一个表

$('#myid > tbody > tr').length

另外,您已经在使用ID选择器,因此无需拥有table#myid

于 2012-08-30T13:48:15.870 回答
1

你所拥有的就是你应该使用的,只要有正确的 id:

$("table[id='myid'] > tbody > tr").length

或者

$("table#myid > tbody > tr").length

演示:http: //jsfiddle.net/Guffa/bbV3Z/

由于 id 在页面中应该是唯一的,因此您应该能够只使用:

$("#myid > tbody > tr").length

#myid > tbody > tr使用而不仅仅是使用的原因#myid tr是确保您只获取该特定表中的行,而不是嵌套在其中的表中的行。如果您没有嵌套任何表格,则可以使用:

$("#myid tr").length
于 2012-08-30T13:52:27.240 回答
0

您可以使用$("#table_id tr").length

于 2012-08-30T13:49:34.460 回答
0

使用var count = $('#myid tr').length;.

于 2012-08-30T13:49:47.477 回答
0

首先考虑上下文来考虑表演:

$('#myid').find('tr')

这是性能比较: http ://jsperf.com/tr-number

于 2012-08-30T13:57:34.600 回答