Set up a table. When the mouse moves to a certain row, the color changes. When the mouse leaves, the color changes to the original color. Each line requires two mouse events:
1. Mouse override: onMouseOver
2. Mouse out: onMouseOut
At the same time, these two events need to call the event function:
1. To set the color of the row:
resetColor(row)
2. Implement the change of row color:
changeColor(row)
Implementation code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"> <html> <head> <title>变色表格示例</title> <script type="text/javascript"> function changeColor(row){ document.getElementById(row).style.backgroundColor='#9999FF'; } function resetColor(row){ document.getElementById(row).style.backgroundColor=''; } </script> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <table width="200" border="1" cellspacing="1" cellpadding="1" align="center"> <tr><th>学校</th><th>专业</th><th>人数</th></tr> <tr align="center" id="row1" onMouseOver="changeColor('row1')" onMouseOut="resetColor('row1')"> <th>北大</th><th>法律</th><th>2000</th> </tr> <tr align="center" id="row2" onMouseOver="changeColor('row2')" onMouseOut="resetColor('row2')"> <th>清华</th><th>计算机</th><th>5000</th> </tr> <tr align="center" id="row3" onMouseOver="changeColor('row3')" onMouseOut="resetColor('row3')"> <th>人大</th><th>经济</th><th>6000</th> </tr> </table> </body> </html>
The above is the detailed content of How to use JavaScript+CSS+DIV to change the color of the table. For more information, please follow other related articles on the PHP Chinese website!