标签:javascrip 联想 width 选中行 head 选中 div hit 选择
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=GB2312" /> 4 <title>table选中的行以指定颜色高亮显示</title> 5 <script type="text/javascript"> 6 function IniEvent() { 7 var tbl = document.getElementById("tblMain"); 8 var trs = tbl.getElementsByTagName("tr"); //getElementsByTagName(按标记名获取元素) 9 for (var i = 0; i < trs.length; i++) { 10 trs[i].onclick = TrOnClick; 11 } 12 } 13 function TrOnClick() { 14 var tbl = document.getElementById("tblMain"); 15 var trs = tbl.getElementsByTagName("tr"); 16 for (var i = 0; i < trs.length; i++) { 17 if (trs[i] == this) { //判断是不是当前选择的行 18 trs[i].style.background = "yellow"; 19 } 20 else { 21 trs[i].style.background = "white"; 22 } 23 } 24 } 25 </script> 26 </head> 27 <body onload="IniEvent()"> 28 <table id="tblMain" border="1" style="width:200px;border-collapse:collapse"> 29 <tr> 30 <td>1</td> 31 <td>三星</td> 32 <td>AA</td> 33 </tr> 34 <tr> 35 <td>2</td> 36 <td>华为</td> 37 <td>BB</td> 38 </tr> 39 <tr> 40 <td>3</td> 41 <td>苹果</td> 42 <td>CC</td> 43 </tr> 44 <tr> 45 <td>4</td> 46 <td>联想</td> 47 <td>DD</td> 48 </tr> 49 </table> 50 </body> 51 </html>
标签:javascrip 联想 width 选中行 head 选中 div hit 选择
原文地址:https://www.cnblogs.com/AsCrazy/p/11538306.html