简体   繁体   English

每两行更改一次,而不是每个表行更改一次

[英]change every 2 rows instead of every table row

I have this code for changing the class from odd to even for every row. 我有这段代码用于将每行的类从奇数更改为偶数。 What i would like it to do is go even even odd odd instead of even odd even odd: 我想要做的是偶数奇数而不是奇数:

window.addEvent('domready', function() {

var count = 0;

$$('table.pretty-table tr').each(function(el) {

el.addClass(count++ % 2 == 0 ? 'odd' : 'even');

});

});
el.addClass(count++ % 4 > 1 ? 'odd' : 'even');

you can use slick. 您可以使用光滑。

$$("table.pretty-table tr:nth-child(4n), table.pretty-table tr:nth-child(4n-1)").addClass("even");

simple. 简单。 http://www.w3.org/TR/css3-selectors/#structural-pseudos http://www.w3.org/TR/css3-selectors/#structural-pseudos

in action: http://www.jsfiddle.net/dimitar/mdtVB/5/ 实际应用: http//www.jsfiddle.net/dimitar/mdtVB/5/

incidentally, it got me wondering if using slick will be faster than the .each loop so i did a little tester class: 顺便说一句,让我想知道使用slick是否会比.each循环更快,所以我做了一个测试器类:

http://www.jsfiddle.net/dimitar/mdtVB/6/ http://www.jsfiddle.net/dimitar/mdtVB/6/

slick runs first for 10000 iterations and 10 secs after load, it runs the .each on it too. slick首先运行10000次迭代,加载后10秒钟,它也运行.each。 in FF 3.6.12 on a windows box, slick wins but marginally. 在Windows Box上的FF 3.6.12中,光滑赢得了胜利,但微不足道。 calling the table by #id will make a difference too in favour of Slick - http://www.jsfiddle.net/dimitar/mdtVB/8/ (with an added delay of 2 sec before testing start for running jsfiddle properly). 通过#id调用表也会对Slick- http ://www.jsfiddle.net/dimitar/mdtVB/8/有所帮助(在开始正确运行jsfiddle之前需要等待2秒)。

No need of a var count also 也不需要变数

window.addEvent('domready', function() {
    $$('table.pretty-table tr').each(function(el, idx) {
        el.addClass(idx % 4 > 1 ? 'odd' : 'even');
    });
});

PS: Just optimizing the answer of The Scrum Meister. PS:只是优化Scrum Meister的答案。

window.addEvent('domready', function() {

    var count = 0;

    $$('table.pretty-table tr').each(function(el) {
        if ( count == 0 || count % 4 < 2) {
            el.addClass( 'odd' );
        }
        else
        {
            el.addClass( 'even' );
        }
    });
    count++;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM