1、前端頁面
2、jq方法
//$(function(){}) 頁面加載後執行,這樣才能獲取到標籤內容
$(function(){
var urlstr = location.href; //當前瀏覽器地址
var urlstatus = false;//定義狀態值
//循環導航a標籤
$("#clearfix a").each(function(key) {
console.log(urlstr);
console.log($(this).attr('href'));
console.log((urlstr + '/').indexOf($(this).attr('href')));
//判斷當前地址是否包含 導航欄 -1是不包含
if((urlstr + '/').indexOf($(this).attr('href')) > -1 && $(this).attr('href')!="") {
urlstatus = true;
$("#clearfix li").eq(key).addClass('active');
}else {
$("#clearfix li").eq(key).removeClass('active');
}
});
//當直接訪問域名時,或者所有導航都未選中時默認選中首頁
if (!urlstatus) {
$("#clearfix li").eq(0).addClass('active');
}
});