设置一个表格,当鼠标移动到某行时,颜色改变,鼠标离开时,颜色变为原来的样子。每一行都要两个鼠标事件:
1.鼠标覆盖:onMouseOver
2.鼠标离开:onMouseOut
同时这两个事件需要调用事件函数:
1.实现对行的颜色设置:
resetColor(row)
2.实现对行颜色的改变:
changeColor(row)
实现代码:
<!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>
Atas ialah kandungan terperinci 如何用JavaScript+CSS+DIV实现表格变色. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!