首页 > 代码库 > javascript实现当前页导航激活

javascript实现当前页导航激活

html

<ul id=”nav”><li><a href=http://www.mamicode.com/”http://www.daqianduan.com/”>首页>

javascript(给当前页面指定导航加了.on的class)

$(function() {var a1 = document.URL;var a2 = $(“#nav a”);for (var i = 0; i < a2.length; i++) {if (a1.indexOf($(a2[i]).attr(“href”)) != -1) {$(a2[i]).parent().addClass(“on”);return;}}$(a2[0]).parent().addClass(“on”);})

css(根据自己的需求做相关调整)

#nav li{display:inline-block;float:left;text-align:center;height:36px;padding-left:4px;line-height:36px;background:url(http://img.daqianduan.com/ui/nav.gif) no-repeat right bottom;}#nav li a{display:block;color:#777;padding:0 15px 0 10px;}#nav li.on{font-weight:bold;background:url(http://img.daqianduan.com/ui/nav.gif) no-repeat 0 0;margin-left:-3px;}#nav li.on a{background:url(http://img.daqianduan.com/ui/nav.gif) no-repeat right 0;}