标签下划线的切换,默认选中第一个,点击的时候切换

<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title>导航栏</title>
	</head>
	<body>
		<style>
			*{
				box-sizing: border-box;
			}
			li {
				list-style: none;
			}
			.nav {
				width: 500px;
				height: 60px;
				margin: 0 auto;
			}
			.nav ul {
				display: flex;
				justify-content: space-around;
				align-items: center;
			}
			.nav li{
				height: 60px;
				line-height: 60px;
			}
			.nav li:hover{
				cursor: pointer;
				/* border-bottom:5px solid #109dff; */
			}
			.active{
				border-bottom:5px solid #109dff;
			}
		</style>
		<div class="nav">
			<ul>
				<li class="active">首页</li>
				<li>我的</li>
				<li>帮组中心</li>
				<li>设置</li>
			</ul>
		</div>
		<script>
			let lis = document.querySelectorAll('.nav>ul>li')
			console.log(lis)
			for (var i = 0; i < lis.length; i++) {
				lis[i].onclick = function(event) {
					let li = document.getElementsByClassName('active')[0]
					li.classList.remove('active')
					event.target.classList.add('active')
				}
			}
		</script>
 
	</body>
</html>
 

标签下划线的切换,默认选中第一个,点击的时候切换

上一篇:伪类选择器


下一篇:springboot多环境配置文件及jar包部署