---- AI试用 ---域名问题某些图片和js资源无法访问,导致一些代码实例无法运行!(代码里gzui.net换成momen.vip即可)

给页面导航栏选中栏目添加特殊样式(页面刷新后仍有效)

javascript 蚂蚁 11914℃ 0评论

html

<div class="nav">
	<ul>
		<li><a href="index.html">Index</a></li>
		<li><a href="posts.html">Posts</a></li>
		<li><a href="comments.html">Comments</a></li>
		<li><a href="links.html">Likes</a></li>
	</ul>
</div>

js

<script>
$(document).ready(function(){
$('.nav li a').each(function(){
	if($($(this))[0].href==String(window.location))
		$(this).parent().addClass('active');
});
})
</script>

后来在多级导航不行 这里改了一下 $(this).attr("href")

<script>
$(document).ready(function() {
	$('.has-sub ul li a').each(function() {
		if ($(this).attr("href") == String(window.location.pathname)){
			$('.main-menu li').removeClass('opened active');
			$(this).parent().addClass('active');
			$(this).parent().parent().parent().addClass('opened active');
		}
	});
})
</script>

案例链接 密码: wdy3

转载请注明:有爱前端 » 给页面导航栏选中栏目添加特殊样式(页面刷新后仍有效)

喜欢 (4)or分享 (0)

(4)个小伙伴在吐槽
  1. 这个我看了,感觉还是有作用的,能帮助访客确定自己的位置。。
    seo小赛2015-05-27 18:32