HTML part of the code:
< ;li>Change the background color when the mouse passes over
- Change the background color when the mouse passes over
- Change the background color when the mouse passes over
- Change the background color when the mouse passes over
Javascript part of the code:
When the mouse passes over, add li class="current" class, when the mouse leaves, remove this class to achieve the purpose of changing the background color
window.onload = function() {
var lis = document.getElementsByTagName("li");
for (var i=0; ilis[i].onmouseover = function() {
this.setAttribute("class", "current");
}
lis[i].onmouseout = function() {
this.setAttribute("class", "");
}
}
}
Css part code:
ul li.current {
background-color:red;
cursor:pointer;
}
The above code is mainly to facilitate everyone to know more about js. A css-implemented version is provided below.