选tr偶数行,JQuery< tr>条纹奇数/偶数行

quick JQuery question, I've got a products page that the user can filter. Each time a filter is applied/removed, a change event calls stripeTable() . I tried to implement the table striping using the following function, however after removing an item and calling stripTable(), the striping did not remain consistent, i.e each visible odd row one colour, each visible even row another.

function stripeTable() {

// Find all odd visible table rows and add .odd class.

$("#resultsTable > tbody > tr:even:visible").each(function() {

$(this).addClass('even');

});

// Find all even visible table rows and add .even class.

$("#resultsTable > tbody > tr:odd:visible").each(function() {

$(this).addClass('odd');

});

}

I cannot work out why the above wouldn't work. I managed to implement the function as below and it works fine. Any ideas?

function stripeTable() {

var count = 1;

// get all visible table rows

$("#resultsTable > tbody > tr").each(function () {

// If table row is visible, strip accordingly.

// Row 0 (table headers) not striped.

if ($(this).is(":visible") && (this.rowIndex !== 0)) {

if ((count % 2) != 0) {

// Remove class .even if applied previously

$(this).removeClass("even");

// Odd row, add class .odd

$(this).addClass("odd");

count++;

} else {

// Remove class .odd if applied previously

$(this).removeClass("odd");

// Even row, add class .even

$(this).addClass("even");

count++;

}

}

});

}

For clarity, stripeTable() is the last function called, the table rows are hidden/shown in the table before hand. Thanks.

解决方案

The order of the psuedo-selectors matters.

// Computes "even" first, then "visible"

"#resultsTable > tbody > tr:even:visible"

// Compute "visible" first, then "even"

"#resultsTable > tbody > tr:visible:even"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值