调用jquery写导航
html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery.min.js"></script>
<style>
.menu { padding:0; margin:0; list-style-type:none;}
.menu li { background:#FFD1A4; margin-right:1px; float:left; color:#fff; }
.menu li a { display:block; width:80px; text-align:center; height:32px; line-height:32px; color:#fff; font-size:13px; text-decoration:none;}
.cur{ background:#D96C00; font-weight:bold;}
</style>
</head>
<body>
<ul class="menu" id="menu">
<li><a href="a.html" target="_blank">首页</a></li>
<li><a href="b.html" target="_blank">SEO优化</a></li>
<li><a href="c.html" target="_blank">生活娱乐</a></li>
</ul>
</body>
</html>
js
<script type="text/javascript">
var urlstr = location.href;
//alert((urlstr + '/').indexOf($(this).attr('href')));
var urlstatus=false;
$("#menu a").each(function () {
if ((urlstr + '/').indexOf($(this).attr('href')) > -1&&$(this).attr('href')!='') {
$(this).addClass('cur'); urlstatus = true;
} else {
$(this).removeClass('cur');
}
});
if (!urlstatus) {$("#menu a").eq(0).addClass('cur'); }
</script>
JS代码实现
html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
#menu li.menu_on{width:100px; height:44px; float:left; text-decoration:none; background:url(menuhover.png) bottom no-repeat}
</style>
</head>
<body>
<div id="menu">
<ul>
<li><a href="/">网站首页</a></li>
<li><a href="/">免费模板</a></li>
</ul>
</div>
</body>
</html>
js
<script type="text/javascript" language="javascript">
var nav = document.getElementById("menu");
var links = nav.getElementsByTagName("li");
var lilen = nav.getElementsByTagName("a");
var currenturl = document.location.href;
var last = 0;
for (var i=0;i<links.length;i++)
{
var linkurl = lilen[i].getAttribute("href");
if(currenturl.indexOf(linkurl)!=-1)
{
last = i;
}
}
links[last].className = "menu_on";
</script>