单页面导航

<style>
*{
margin: 0;
padding: 0;
}
li{
list-style: none;
}
.nav{
width:100%;
height:40px;
background: #222;
}
.last{
width:1000px;
height:40px;
margin: 0 auto;
}
.last li{
float: left;
}
.last li a {
color: #aaa;
padding: 0 30px;
line-height: 40px;
text-decoration: none;
display: block;
}
.last li a:hover{
background: #333;
color: #fff;
}
.last li a.on{
background: #333;
color: #fff;
}
</style>
<script src="jquery-1.7.2.js"></script>
<script src="style.js"></script>
</head>
<body>
<div class="nav">
<ul class="last">
<li><a href="index.html">1</a></li>
<li><a href="index2.html">2</a></li>
<li><a href="index3.html">3</a></li>
<li><a href="index4.html">4</a></li>
</ul>
</div>
<h1>1</h1>
</body>
</html>

index2-4的布局都是一样的。
style.js

$(function(){    
        var index=window.location.href.split('/').length-1;    
        var href=window.location.href.split('/')[index];    
        $('.last li a[href="'+href+'"]').addClass('on');
});
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,809评论 1 92
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,462评论 25 708
  • 常用名词说明 Schedulers(调度器) 解决Android主线程问题; 解决多线程线程问题 Observab...
    浮名虚誉架构师阅读 150评论 0 0
  • *六月飞雪 一站又一站,列车鸣笛, 先生醉卧车厢,一路风尘。 雾䑃朦,雨朦朦, 一路征尘,心舒畅, 顶天立地,斗风...
    朝花夕拾123阅读 234评论 0 2
  • 女:你的微笑 会说话 甜蜜融化我的心 男:多想收藏 你的情绪 喜怒哀乐真实的你 女:你用温柔 轻轻捧起我的心...
    UnaFung阅读 99评论 0 1