• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:テーブルの背景を変える)

テーブルの背景色を変える方法

このQ&Aのポイント
  • JavaScriptを使ってテーブルの背景色を変える方法について教えてください。
  • 具体的には、テーブルの1段目と3段目のセルを赤色に、2段目と4段目のセルを青色に変えたいです。
  • 奇数のセルが赤色、偶数のセルが青色になるようにしたいです。

質問者が選んだベストアンサー

  • ベストアンサー
  • BLUEPIXY
  • ベストアンサー率50% (3003/5914)
回答No.1

外部ファイル部分 --------------------------------------------------------------- function stripe(id){ var tb = document.getElementById(id); for(var r=0;r < tb.rows.length;r++){ if(r % 2 == 0){//r: 0始まり tb.rows[r].style.backgroundColor="red";//奇数行が赤 } else { tb.rows[r].style.backgroundColor="blue";//偶数行が青 } } } ---------------------------------------------------------------- このページでの処理するテーブルを指定しての呼出 <body onload="stripe('TABLE1')"> 処理テーブルにIDを振る <table border="1" id="TABLE1">

sankou
質問者

補足

大変ありがとうございます。とても参考になりました。ただ、もう一つ教えてもらえばと思います。外部ファイル(js)でお願いします。参照ボタンがあります。何のファイルでもいいですが選択して選びます。そうしますとテキストボックスの中にファイル名が入ります。OKボタンを押すと下にある参照、参照ファイル名、参加者名の下に上記で選択した内容が表示(アップロード)される。NOを押すと削除されると言うやり方は可能なのでしょうか? <html> <head> <title>画面</title> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> </head> <body> <table border="1"> <col width="50"> <tr> <th> 参照 </th> <td > <input type="file" name="file"size="30"> </td> </tr> </table> <td><input type="button" value="OK"></td> <td><input type="button" value="NO"></td> <table border="1"> <col width="85"> <tr> <th>参照</th> <th>参照ファイル名</th> <th>参加者名</th> </tr> </table> </body> </html>

全文を見る
すると、全ての回答が全文表示されます。

その他の回答 (1)

  • BLUEPIXY
  • ベストアンサー率50% (3003/5914)
回答No.2

#1補>可能なのでしょうか? テーブルに行(列)を追加及び削除するのは、参考URLのようなやり方できます(同じ人ですよね?) * 補足のソースリストでは、参照と参加者名の元データになる部分がありません。

参考URL:
http://okwave.jp/kotaeru.php3?qid=1797452
全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • テーブルのテーブルの間に間ができるのでしょうか?

    はじめまして、超html初心者のものです。 なぜ同じようテーブルを3つ書いているのに一番上のテーブルと真ん中のテーブルの間が開いてしまっているのかがわかりません。 あと、一番上のテーブルの-から先をもう少し左に寄せることもうまくいきません。 なにかアドバイスをよろしくお願いします。 ↓これがソースです。 <html> <head><title>test</title> </head> <form> <table border=1 cellspacing=0 cellpadding=2 width=760> <tr> <th width=95 height=30 align="right">日付け:</th> <td width=284> <input type="text"><input type="button"> </td> <td>&nbsp;-&nbsp;</td> <td><input type="text"><input type="button"></td> </tr> </table> <table border=1 cellspacing=0 cellpadding=2 width=760> <tr> <th width=95 height=30 align="right">部署:</th> <td width=100> <select name="busyo"></select> </td> <th width=80 align="right">営業所:</th> <td width=100> <select name="eigyousyo"></select> </td> <th width= 80 align="right">担当コード:</th> <td width=450> <select name="tantocode"></select> </td>   </tr> </table> <table border=1cellspacing=0cellpadding=2width=760> <tr> <th width=80 align="right">権限:</th> <td width=700> <select name="kengen"></select> </td> </tr> </table> </form> </html>

  • cssでテーブルの位置を設定するには?

    <table border="1" align="left">でテーブルの位置を競ってすることはできたのですが スタイルシートで行いたいです。 そもそも他のテーブルはすべて中央に寄せたいため、 table { margin-left: auto; margin-right: auto; } としています。 そのうえで、任意のテーブルのみ左に寄せて表示したいので table.test { align:left; } としたのですが、うまくいきません。 ---------------------------------------------------------- <html> <head> <title>test</title> <STYLE type="text/css"> table { margin-left: auto; margin-right: auto; width: 40%; } table.test { width: 80%; align:left; } </STYLE> </head> <body> <table border="1"> <tr><th>見出</th><th>見出</th></tr> <tr><td>セル</td><td>セル</td></tr> </table> <br><br><br> <table border="1" align="left"> <tr><th>見出</th><th>見出</th></tr> <tr><td>セル</td><td>セル</td></tr> </table> <br><br><br> <table border="1" class="test"> <tr><th>見出</th><th>見出</th></tr> <tr><td>セル</td><td>セル</td></tr> </table> </body> </html> ----------------------------------------------------- このコードだと、 table.test { width: 80%; align:left; } の、 width: 80%; の部分は認識されるようですが、align:left;は無視されます。 スタイルシートでテーブルの位置を設定する方法をご教授ください。

    • 締切済み
    • CSS
  • DreamWeaverで作るテーブル

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=shift_jis"> <title>無題ドキュメント</title> </head> <body> <table width="800" height="394" border="1" align="center"> <tr> <td>  <p>1</p> <table width="200" border="1" align="left"> <tr> <td height="118">2</td> </tr> </table> <table width="600" border="1"> <tr> <td height="121">3</td> </tr> </table> <p> </p></td> </tr> </table> </body> </html> このようにテーブルを作った場合、3のセルの右のラインが1のセルの右ラインに隠れるというか見えなくなります。 そこで3のセルにalign="left"を設定するとIEではラインが見えるのですが、Netscapeでは2と3が縦に並んでしまいます。 なんとか3の右ラインを見えるようにしてNetscapeでも縦に並ばないようにするにはどうすればいいでしょう。 よろしくお願いいたします。

  • Tableタグ内のspan styleが適応されない

    先日ブログを開設して、表を作りたかったのでtableタグを使ったのですが、表中の文字が何故か10ptにならず、およそ15ptくらい大きくなってしまいます。 どこかスタイルシート(HTML?)がおかしいのでしょうか? 詳しい方、ご教授の程お願いいたします。 下記の様に設定しました。 <table border="1" cellpadding="0" cellspacing="0" width="430"><colgroup> <col align="center"><col span="5" align="center"> <span style="font-size:10pt;"><tr><th>・・・ ・・・</td></tr></span></col></colgroup></table>

    • ベストアンサー
    • HTML
  • 入れ子にしたテーブルをheight100%にする

    お世話になります。 以下のソースで、table2の高さを100%にしたいです。 この書き方では、firefoxではうまく表示できるのですが、 IEではだめでした。どこがいけないのでしょうか?? ---------------------------------- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <style type="text/css"> <!-- html {height:100%;} body {height:100%;} .th100 {height:100%;} --> </style> </head> <body> <table width="600" border="1" cellpadding="0" cellspacing="0" class="th100"> <tr> <td width="300" rowspan="2"> 長いテキスト </td> <td height="50"><table width="300" height="50" border="0" cellpadding="0" cellspacing="0" bgcolor="#000000"> <tr> <td>table1</td> </tr> </table></td> </tr> <tr> <td><table width="300" border="0" cellpadding="0" cellspacing="0" bgcolor="#999999" class="th100"> <tr> <td>table2</td> </tr> </table></td> </tr> </table> </body> </html>

    • ベストアンサー
    • HTML
  • ファイルの内容が表示できるか?

    外部ファイルでお願いします。 参照ボタンから、何でもいいのでファイルを選びます。ファイル名がテキストボックスに表示されます。 下にあるYESボタンを押すと、その下にコメントアウト してあるテキストボックスが上記のID,ファイル名に追加されテキストボックスの中に参照ボタンから選んだ ファイルが挿入される。また、左のNOボタンを押すと その列が削除されるというやり方は可能でしょうか? <html> <head> <title>画面</title> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> </head> <body> <table> <col width="100"> <col width="100"> <tr> <td> <input type="file" name="file"size="50"> </td> </tr> </table> <table border ="1"> <col width="50"> <col width="200"> <tr> <th>ID</th> <th>ファイル名</th> </tr> </table> <input type="button" name="yes" value="YES"value="" > <!-- <table border="1"> <col width="50"> <col width="200"> <tr> <td><input type="button" name="no"value="NO"value="" ></td> <td><input type="text" name="file" size="40" value=""></td> </tr> </table> --> </body> </html>

  • 動的なtableの値を取得したい

    いつもお世話になります。 見よう見まねでやっている超初心者です。 下記の様なテーブル(phpで読み込んでいますので実際は動的です。) の回答ボタンをクリックすると テーブル下の<INPUT type="text" name="kotae">に ボタンをクリックされた行のb列の値を入れたいと思っています。 function kaito()をどのように書いたら取得できるか悩んでいます。 初心者なもので具体的な回答でしたら助かります。 ご教授の程どうぞよろしくお願い致します。 <html> <head> <meta content="text/html; charset=utf-8" /> <script> </script> </head> <body> <table width="100" border="1"> <tr> <th width="30" scope="col">a</th> <th width="30" scope="col">b</th> <th width="40" scope="col">c</th> </tr> <tr> <td>1</td> <td>2</td> <td><input type="button" value="回答" onclick="kaito()"/></td> </tr> <tr> <td>5</td><td>3</td> <td><input type="button" value="回答" onclick="kaito()"/></td> </tr> <tr> <td>5</td> <td>4</td> <td><input type="button" value="回答" onclick="kaito()"/></td> </tr> <tr> <td>5</td> <td>5</td> <td><input type="button" value="回答" onclick="kaito()"/></td> </tr> </table> <INPUT type="text" name="kotae" size="12" > </body> </html>

  • CSSのテーブルについて教えて下さい。

    テーブルなのですがh1が適用されません。 又表の枠線が黒になりません。 また(休診)を赤にしたいのですが表示されません。 どこをどう直したらいいのか教えて下さい。 お願いします。 h1 { margin: 1em 0; padding: 0.3em; border: 1px silver solid; background: url(../image/subpage_h2_bg.gif) bottom repeat-x; color: gray; font-weight: bold; font-size: large; } /*hyou*/ caption{ color: #996666; padding-bottom: 14px; text-align: center; } table{ width: 800px; border: solid 2px ; border-collapse: collapse; } th,td { padding: 5px; border: solid 1px #000000; text-align:center; } th { background-color:silver; font-weight: bold; } td.col01{ font-weight: bold; background-color:silver; } em { color: red; font-weight: bold; } /* テーブル列幅指定 */ .cola { width:100px; } .col01{ width: 100px; } .col02{ width: 70px; } .col03{ width: 70px; } .col04{ width: 70px; } .col05{ width: 70px; } .clo06{ width: 70px; } .style1 {margin-left:1em; } HTML <h1>外来診療のご案内</h1> <table> <caption>外来診療 予定表</caption> <tr> <th class="a">診察科</th> <th class="b">月</th> <th class="c">火</th> <th class="d">水</th> <th class="e">木</th> <th class="f">金</th> <th class="g">土</th> <th class="h">日</th> </tr> <tr> <th class="col01">一般歯科</th> <td class="col02" rowspan="4"><em>休診</em></td> <td class="col03" colspan="5">◯</td> <td class="col04" rowspan="4"><em>休診</em></td> </tr> <tr> <th class="col01">小児歯科</th> <td class="col02">◯</td> <td class="col03">&nbsp;</td> <td class="col04">◯</td> <td class="col05">&nbsp;</td> <td class="col06">◯</td> </tr> <tr> <th class="col01">歯列矯正</th> <td class="col02">&nbsp;</td> <td class="col03">◯</td> <td class="col04">&nbsp;</td> <td class="col05">◯</td> <td class="col06">&nbsp;</td> </tr> <tr> <th class="col01">審美歯科</th> <td class="col02">&nbsp;</td> <td class="col03">◯</td> <td class="col04">&nbsp;</td> <td class="col05">&nbsp;</td> <td class="col06">◯</td> </tr> </table>

    • ベストアンサー
    • HTML
  • vb2005でwebbrowserに表示したテーブルタグの値を取得したい。

    <head> <meta http-equiv="Content-Type" content="text/html; charset=shift_jis" /> <title>無題ドキュメント</title> </head> <body> <table width="450" border="1"> <tr> <th scope="col">&nbsp;</th> <th scope="col">&nbsp;</th> </tr> <tr> <th scope="row">&nbsp;</th> <td><table width="450" border="1"> <tr> <th scope="col">&nbsp;</th> <th scope="col">&nbsp;</th> </tr> <tr> <th scope="row">&nbsp;</th> <td>【xyz】</td> </tr> </table></td> </tr> </table> </body> </html> このようなhtml文書があったとして、 【xyz】この値を取得したいのですが WebBrowser1.Navigate("上記のhtml文書のURL") Do While (WebBrowser1.IsBusy Or WebBrowser1.ReadyState <> WebBrowserReadyState.Complete) My.Application.DoEvents() System.Threading.Thread.Sleep(50) Loop からどのようにコーディングすれば取得できるんでしょうか?できるだけ文字列操作をするのではなくて、 用意された2.0のクラス群を利用して取得したいんです。教えてください。お願いします。

  • 一気に「テーブルの2列目のtdタグを右詰にする

    <html> <head> <title>test</title> </head> <body> <table border=1> <tr><th>No</th><th>aaaaaaa</th></tr> <tr><td>1</td><td>bbb</td></tr> <tr><td>2</td><td>ccc</td></tr> </table> </body> </html> このようなテーブルで 右側の2列目のtdタグを右詰にしたいのですが、 一気に「テーブルの2列目のtdタグを右詰にする」と言う方法は有りますか? 今は <html> <head> <title>test</title> <style type="text/css"> td.example1 { text-align: right; } </style> </head> <body> <table border=1> <tr><th>No</th><th>aaaaaaa</th></tr> <tr><td>1</td><td class="example1">bbb</td></tr> <tr><td>2</td><td class="example1">ccc</td></tr> </table> </body> </html> このようにしていますが 一つ一つタグを付けるのは非効率的と思っています。

    • ベストアンサー
    • HTML