JavaScript实例---表格隔行变色以及移入鼠标高亮
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>index</title> <style type="text/css"> #div1{width: 100px;height:100px;background: red;} </style> <script type="text/javascript"> window.onload = function() { var oTab = document.getElementById(‘tab1‘); var oldColor = ‘‘; for(var i=0;i<oTab.tBodies[0].rows.length;i++) { oTab.tBodies[0].rows[i].onmouseover = function() { oldColor = this.style.background; this.style.background = "green"; } oTab.tBodies[0].rows[i].onmouseout = function() { this.style.background = oldColor; } if(i%2) { oTab.tBodies[0].rows[i].style.background = ‘red‘; } else { oTab.tBodies[0].rows[i].style.background= " "; } } } </script> </head> <body> <table border="1" width="300" id="tab1"> <thead> <td>ID</td> <td>NAME</td> <td>AGE</td> </thead> <tbody> <tr> <td>1</td> <td>John</td> <td>27</td> </tr> <tr> <td>2</td> <td>Lucy</td> <td>27</td> </tr> <tr> <td>3</td> <td>Dan</td> <td>26</td> </tr> <tr> <td>4</td> <td>Ben</td> <td>30</td> </tr> </tbody> </table> </body> </html>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。