1。説明
(1)指定された要素のID値に従って参照またはノードを要素に返します
document.getElementByID( "TR_TH")
(2)パラメーターのマークに従って参照またはノードを要素のグループに返します
document.getElementsByTagname( "TD")
2。ソースコードを実装します
<!doctype html public " - // w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title> javascript style speal id and name </title </<stely type = " 1px #ccccccソリッド; } </style> <script type = "text/javascript">/*** javaScriptスタイルを介してid and name*/function setFontColor(){//テーブルでヘッダーIDを取得= document.getElementByID( "tr_th"); //ヘッダーテキストの色を設定しますtabid.style.color = "yellow"; // td var tabname = document.getElementsByTagname( "TD")を取得します。 // td var len = tabname.lengthの数を計算します。 // TDをテーブルに送信し、TDのコンテンツ色を(var i = 0; i <len; i ++){tabname [i] .style.color = "blue"; }} </script> </head> <body> <table cellpadding = "1" cellpacing = "0" style = "border:" border:1px #cccccc solid; width:50%; center; "> <tr id =" tr_th "> <th> work number </th> <th> <td> 2012010101 </td> <td> zhang sansan </td> <td> 23 </td> <td>男性</td> </tr> <tr> <td> 2012010102 </td> <td> liu sisi </td> <td> type = "button" value = "set color" onclick = "setFontColor()"/> </body> </html>3。結果を達成します
(1)初期化
(2)[色の設定]ボタンをクリックします