html如何实现类似功能?默认选择是“首页”时,“首页 ”有背景色,点击其他时,其他有背景色,如图
2个回答
展开全部
<!DOCTYPE HTML>
<html>
<meta charset="UTF-8">
<title>test</title>
<head>
<style type="text/css">
div {
text-align: center;
width: 200px;
height: 50px;
line-height: 50px;
font-size: 15px;
background: darkblue;
}
a {
display: inline-block;
height: 50px;
width: 50px;
color: white;
font-weight: bold;
text-decoration: none;
}
a:hover {
background: red;
}
.active {
background: red;
}
</style>
<script type="text/javascript">
window.onload = function() {
var as = document.getElementsByTagName('A');
for(var i in as) {
var a = as[i];
a.onclick = function() {
removeActive();
this.classList.add('active')
};
}
var removeActive = function() {
for(var i in as) {
as[i].classList && as[i].classList.remove('active');
}
}
}
</script>
</head>
<body>
<div>
<a href="#" class="active">首页</a>
<a href="#">百家</a>
<a href="#">财经</a>
</div>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询