1

ボタンをクリックすると、私のコードはすべてのテーブルの色のみを変更します。各セルの間にスペースがあることに注意してください。テーブル自体ではなく、テーブル内のすべてのセルの色を変更する必要があることがわかりました。どんな提案でも、私はJSとCSSが初めてです。

 <script type="text/javascript">
 function color(){
    document.getElementById('mytable').style.backgroundColor ="blue";
 }
 </script>
4

3 に答える 3

1

関数の new を別のものに変更することをお勧めしますclear()-インラインで関数名として使用すると、この単語が機能するとは思いません。これは、@Dennis が述べたように、 document.clear前のチェーンで見つかるためwindow.clearです。詳細については、この質問を参照してください。


これは動作中の jsFiddle です。

これを行う 1 つの方法は次のようになります。

function color(){
   var x = document.getElementsByTagName('td');
   for(i=0;i<x.length;i++) {
     x[i].style.backgroundColor ="blue";
   }
}

function clearit(){
   var x = document.getElementsByTagName('td');
   for(i=0;i<x.length;i++) {
     x[i].style.backgroundColor = "";
   }
}
于 2013-05-05T17:09:06.583 に答える
1

あなたはこれを行うことができます:

CSS

td.blueCol { background-color: blue; }

JS

function color() {
    var tds = document.getElementById('mytable').getElementsByTagName('td');
    for (var i = 0; i < tds.length; i++) {
        // set a class which defines the background color
        tds[i].className = "blueCol";
    }
}

function clear() {
    var tds = document.getElementById('mytable').getElementsByTagName('td');
    for (var i = 0; i < tds.length; i++) {
        // re-set a class which defines the background color
        tds[i].className = "";
    }
}
于 2013-05-05T17:09:33.450 に答える