我目前使用此代码将一个类添加到我表中的每一行。使用jquery选择隔行2表行
$(".stripeMe tr:even").addClass("alt");
然而,在另一个表我想添加一个类行3,4,7,8,11,12等等...
这可能吗?
我目前使用此代码将一个类添加到我表中的每一行。使用jquery选择隔行2表行
$(".stripeMe tr:even").addClass("alt");
然而,在另一个表我想添加一个类行3,4,7,8,11,12等等...
这可能吗?
你需要做的是这样的:
$(".stripeMe tr:nth-child(4n)").add(".stripeMe tr:nth-child(4n-1)").addClass("alt");
//or...
$("tr:nth-child(4n), tr:nth-child(4n-1)", ".stripeMe").addClass("alt");
You can see this working here。
使用此:
$(".stripeMe tr:nth-child(4n), .stripeMe tr:nth-child(4n-1)").addClass("alt");
gets different results(即WebKit中,可能还有其他人)。
随着`:第n-child'选择:http://api.jquery.com/nth-child-selector/
$(".stripeMe tr:nth-child(4n), .stripeMe tr:nth-child(4n-1)").addClass("alt");
这不工作:) http://jsfiddle.net/ndn67/ – 2010-05-06 15:11:05
你的例子适合我... – RoToRa 2010-05-06 15:14:01
@RoToRa - Ope它在webkit :) – 2010-05-06 15:14:49
可以使用filter
功能来过滤设定你喜欢的任何方式:
$(".stripeMe tr")
.filter(function(i){ return (i % 4) >= 2; })
.addClass("alt");
这将保持与指数2,3,6,7,10,11等的项目。请注意,该索引是基于零的,因此第三行为索引二。
我使用for循环和.eq()方法为此问题做了一个不同的方法。
var a = 2; // start from 2 because eq() counts from 0
var b = 3; // start from 3 because eq() counts from 0
var total = $('.stripeMe td').length;
for (i = 0; i <= total; i++){
if (i == a){
$('.stripeMe tr:eq('+a+')').css('background', 'red');
a+=4;
}
else if (i == b){
$('.stripeMe tr:eq('+b+')').css('background', 'blue');
b+=4;
}
};
请提供表结构的一个片段! – 2010-05-06 15:07:23
它的一个简单的表结构: