第1个回答 2020-03-01
<html>
<style>
li {float:left;margin-right:25px;padding:5px}
.active {background-color:red}
.hover {background-color:blue;color:white}
.normal {background-color:white;color:black}
</style>
<body>
<ul id="navbar">
<li class="active" id="first">首页</li>
<li id="second">二页</li>
<li id="third">三页</li>
</ul>
<script type="text/javascript">
var navbar=document.getElementById("navbar");
var items=navbar.getElementsByTagName("li");
var curactiveitem="first";
for(i=0;i<items.length;i++){
items[i].onmouseover=onhover;
items[i].onclick=onactive;
}
function onhover(){
for(i=0;i<items.length;i++){
if(items[i].id!=curactiveitem){
items[i].className="normal";
}
}
if(this.id!=curactiveitem){this.className="hover";}
}
function onactive(){
document.getElementById(curactiveitem).className="normal";
this.className="active";
curactiveitem=this.id;
}
</script>
</body>
</html>