6

这是我的html

<table id="tbl1">
  <tbody >
         <tr class="hide_grid_header"></tr>
         <tr style="display: none;"></tr>
         <tr style="display: none;"></tr>
          <tr ></tr>
         <tr style=""></tr>
         <tr ></tr>
         <tr style=""></tr>
         <tr ></tr>
       <tr style="display: none;"></tr>
  </tbody>
</table>

由此,我想要没有 style 属性或 style=" " 属性的 tr 计数。

我正在使用下面的代码,但它让我算作 8 而不是 5。

 var docs = jQuery("#tbl").find('tbody').find('tr:visible');
alert(docs.length);
4

4 回答 4

4
$('tr').filter(function(){
    return !$(this).attr('style');
}).length;
于 2013-03-15T07:29:14.000 回答
2
var len = $('tr').filter(function(){
      return !$(this).attr('style');
}).length;

http://jsfiddle.net/6pHt6/

于 2013-03-15T07:24:02.573 回答
0

你可以试试.filter()

console.log($('table tr').filter('[style]').length);

.filter()过滤掉对象的集合。

使用过滤器检查小提琴

.not()

console.log($('table tr').not('[style]').length);

This will output all the tr length which doesn't have a style attributes.

未使用的结帐小提琴

于 2013-03-15T07:24:38.637 回答
0

分别选择每个集合

var $tr_blank_style = $('tr[style=""]'); // with style=""
var $tr_no_style    = $('tr:not([style])'); // no style attribute at all
var $tr_count       = $tr_blank_style.length + $tr_no_style.length;


使用filter同时选择两者

var $tr_count = $('tr').filter(function() {
        return $(this).is('[style=""],:not([style])'); 
    }).length;
于 2013-03-15T07:32:10.887 回答