<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');
});