JQuery tr在前5行后添加类 [英] JQuery tr add class after first 5 rows

查看:103
本文介绍了JQuery tr在前5行后添加类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有n个表格。我需要遍历页面上的每个表,并且在每个表中我需要向行添加一个类,而不是前五行。

I have n-tables on a page. I need to go through every table on the page and within each table i need to add a class to the rows but NOT the first 5 rows.

我当前的js:

$('.selector').each(function(){
   var trCount = $("tbody > tr", this).size();
   alert(trCount);
});

这会告诉我每行中有多少tr。但是我需要遍历每一行,如果当前行超过第5行,则向其添加类..

This goes through and tells me how many tr's I have in each row. However I need to then go through each row and if the current row is more than the 5th row then add classes to it..

所以希望我得到以下内容: / p>

So hopefully I get the following:

<table class='selector'>
<tr><td>A</td></tr>
<tr><td>A</td></tr>
<tr><td>A</td></tr>
<tr><td>A</td></tr>
<tr><td>A</td></tr>
<tr class='hidden'><td>A</td></tr>
<tr class='hidden'><td>A</td></tr>
<tr class='hidden'><td>A</td></tr>
<tr class='hidden'><td>A</td></tr>
<tr class='hidden'><td>A</td></tr>
</table>


推荐答案

$("table").each(function() {
  $("tr:gt(4)", this).addClass("hidden");
});

注意: :gt(n) 伪类是从零开始的,所以前五行是o到4。

Note: the :gt(n) pseudo-class is zero-based so the first five rows are o to 4.

这篇关于JQuery tr在前5行后添加类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆