1. 獲得某class的所有對象的方法: (返回的是數組) 這里我是綁定到a鏈接上
var links = document.querySelectorAll("ul > li > a");
或是通過getElementById getElementsByTagName來獲取元素
2. for循環給list對象數組的每個對象添加懸停和懸出事件
for(var i in links) { links[i].onmouseover = changeColor; links[i].onmouseout = delChangeColor; }
3.再定義移出與移入的兩個函數
<script> function changeColor() { this.style.backgroundColor = '#003366'; this.style.color = 'white'; } function delChangeColor() { this.style.backgroundColor = '#f8f8f8'; this.style.color = 'black'; } </script>