jQuery学习日记---第三篇 JS实现导航条点击以后会高亮显示

2023-08-26 18:54:33 浏览数 (1)

下面是实例代码

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="js/jquery-2.2.0.min.js"></script>
    <style>
        #nav li {float: left; width: 80px; text-align: center; list-style-type: none;}
        .nav-active {background: #ff47a5;}
        .nav-active a {color: #fff;text- decoration:none}
    </style>
</head>
<body>
<ul id="nav">
    <li class="nav-active"><a href="javascript:;">首页</a></li>
    <li><a href="javascript:;">Page1</a></li>
    <li><a href="javascript:;">Page2</a></li>
    <li><a href="javascript:;">Page3</a></li>
    <li><a href="javascript:;">Page4</a></li>
</ul>
	<script>
	$('#nav').find('li').click(function() {
		// 为当前点击的导航加上高亮,其余的移除高亮
		$(this).addClass('nav-active').siblings('li').removeClass('nav-active');
	});
	</script>
</body>
</html>

0 人点赞