开发者

How to algorithmically color the opposite diagonal of a table's diagonal

开发者 https://www.devze.com 2023-04-12 11:17 出处:网络
I have a table that I colored boxes that form the diagonal. I am looking how to color algorithmically the opposite diagonal.

I have a table that I colored boxes that form the diagonal.

I am looking how to color algorithmically the opposite diagonal.

Demo : http://jsfiddle.net/pJt7x/

Javascript (1 == 2 must be replaced) :

for (i=0; i<=5; i++) {
    $('table').append('<tr>');

    for (j=0; j<=5; j++)
        ((i == j)||(1 == 2)) ?
            $('tr:last').append('<td class="x"></td>') : 
                $('tr:last').append('<td></td>');

    $('table').append('</tr>'); 
}

Answer : http://jsfiddle.net/pJt7开发者_开发百科x/2/


The opposite diagonal is i + j == 5


Do you need to fill both diagonals? this would work:

for (i=0; i<=5; i++) {
    $('table').append('<tr>');

    for (j=0; j<=5; j++)
        ((i == j) || (i+j == 5)) ?
            $('tr:last').append('<td class="x"></td>') :
                $('tr:last').append('<td></td>');

    $('table').append('</tr>');
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号