1

テーブルを反復処理する方法について以前の回答を見てきましたが、それを機能させることができません。私は愚かなことをしているに違いない。次に例を示します。

私はCSSでこれを持っています

td{width: 30px; height: 30px;
} 

<body onload = "setBackground();"> 

<table id = "table1"> 
    <tr id = "row1">
       <td>Stack</td>
       <td>Overflow</td>
       <td>Is my life saver</td>
    </tr>
</table> 

</body>

今、JSで私はこれを持っています

function setBackground() {

var table = document.getElementById("table1"); 

//i found this in a previous stack overflow answer and tried it

for (var i = 0, row; row = table.rows[i]; i++) { 

   for (var j = 0, col; col = row.cells[j]; j++) { 

  //this is for debugging purposes... I can't even get this to work
   alert(table.rows[i].cells[j]); 

   table.rows[i].cells[j].style.background = "orange"; //just an example

    }  
 }
}

body タグで関数を呼び出していることに注意してください。これは機能していません。私はこれを理解しようとしてきましたが、できません! また、私はJQueryを知りません。誰かが親切に私を助けることができれば、私はそれを感謝します.

4

4 に答える 4

0

jQuery を使用する準備ができている場合は、これを試すことができます。

$('#table1 td').css('background', 'orange');

しかし、もっと複雑な操作を実行したい場合は、.each関数を試してください

$('#table1 td').each(function(){
    $(this).css('background', 'orange');
    //or plain javascript if you prefer
    this.style.background = 'orange';
});

.eachtd関数内のすべての s とキーワードを繰り返し処理しthis、現在ループされている を参照しますtd

于 2013-10-09T10:19:04.593 に答える