tva*_*son 922
使用选择器将选择所有行并获取长度.
var rowCount = $('#myTable tr').length;
Run Code Online (Sandbox Code Playgroud)
注意:这种方法也计算每个嵌套表的所有trs!
小智 173
如果您使用<tbody>或<tfoot>在表中,则必须使用以下语法,否则您将获得不正确的值:
var rowCount = $('#myTable >tbody >tr').length;
Run Code Online (Sandbox Code Playgroud)
Dev*_*One 48
另外...
var rowCount = $('table#myTable tr:last').index() + 1;
Run Code Online (Sandbox Code Playgroud)
这将确保不计算任何嵌套的表行.
jjr*_*man 32
好吧,我从表中获取attr行并获得该集合的长度:
$("#myTable").attr('rows').length;
Run Code Online (Sandbox Code Playgroud)
我认为jQuery的工作量更少.
Ric*_*y G 16
这是我的看法:
//Helper function that gets a count of all the rows <TR> in a table body <TBODY>
$.fn.rowCount = function() {
return $('tr', $(this).find('tbody')).length;
};
Run Code Online (Sandbox Code Playgroud)
用法:
var rowCount = $('#productTypesTable').rowCount();
Run Code Online (Sandbox Code Playgroud)
chi*_*iii 12
我得到以下内容:
jQuery('#tableId').find('tr').index();
Run Code Online (Sandbox Code Playgroud)
我需要一种方法在AJAX返回中执行此操作,所以我写了这篇文章:
<p id="num_results">Number of results: <span></span></p>
<div id="results"></div>
<script type="text/javascript">
$(function(){
ajax();
})
//Function that makes Ajax call out to receive search results
var ajax = function() {
//Setup Ajax
$.ajax({
url: '/path/to/url', //URL to load
type: 'GET', //Type of Ajax call
dataType: 'html', //Type of data to be expected on return
success: function(data) { //Function that manipulates the returned AJAX'ed data
$('#results').html(data); //Load the data into a HTML holder
var $el = $('#results'); //jQuery Object that is holding the results
setTimeout(function(){ //Custom callback function to count the number of results
callBack($el);
});
}
});
}
//Custom Callback function to return the number of results
var callBack = function(el) {
var length = $('tr', $(el)).not('tr:first').length; //Count all TR DOM elements, except the first row (which contains the header information)
$('#num_results span').text(length); //Write the counted results to the DOM
}
</script>
Run Code Online (Sandbox Code Playgroud)
显然这是一个快速的例子,但它可能会有所帮助.
我发现如果你想计算行数而不计算表中的表和行中的任何行,我发现这个工作真的很好:
var rowCount = $("#tableData > tbody").children().length;
Run Code Online (Sandbox Code Playgroud)
小智 6
如果有tbody,试试这个
没有标题
$("#myTable > tbody").children.length
Run Code Online (Sandbox Code Playgroud)
如果有标题那么
$("#myTable > tbody").children.length -1
Run Code Online (Sandbox Code Playgroud)
请享用!!!
row_count = $('#my_table').find('tr').length;
column_count = $('#my_table').find('td').length / row_count;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
662065 次 |
| 最近记录: |