HTML code: viewsourceprint? 1 & lt; ul & gt; & nbsp; 2 & nbsp; & lt; li & gt; change the background color when the mouse passes through & lt; /li & gt; & nbsp; 3 & nbsp; & lt; li & gt; change the background color when the mouse passes through & lt; /li & gt;
Some HTML code:
View sourceprint? 1
2
- Changes the background color when the mouse passes
3
- Changes the background color when the mouse passes
4
- Changes the background color when the mouse passes
5
- Changes the background color when the mouse passes
6
Javascript code:
When the mouse passes, add the class = "current" to li. When the mouse leaves, remove the class to change the background color!
View sourceprint? 01 window. onload = function (){
02 var lis = document. getElementsByTagName ("li ");
03 for (var I = 0; I
04 lis [I]. onmouseover = function (){
05 this. setAttribute ("class", "current ");
06}
07 lis [I]. onmouseout = function (){
08 this. setAttribute ("class ","");
09}
10}
11}
Part of the css code:
View sourceprint? 1 ul li. current {
2 background-color: red;
3 cursor: pointer;
4}