設定一個表格,當滑鼠移到某行時,顏色改變,滑鼠離開時,顏色變成原來的樣子。每一行都要兩個滑鼠事件:
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>
以上是如何用JavaScript+CSS+DIV實作表格變色的詳細內容。更多資訊請關注PHP中文網其他相關文章!