jquery/js当前URL对当前栏目高亮突出显示的两种方案

html:

  • 首 页

 

  • 专家观点

 

  • 课题研讨

 

  • 研究报告

 

 

jquery:

//除了首页外当前URL对当前栏目高亮突出显示
$(“.nav li a:not(:first)”).each(function(){
$this = $(this);
if($this[0].href==String(window.location)){
$this.parent().addClass(“selected”);
}
});
//当前URL对当前栏目高亮突出显示
$(“.sidenav li a”).each(function(){
$this = $(this);
if($this[0].href==String(window.location)){
$this.parent().addClass(“selected”);
}
});

或者原生js:

//js 当前URL对当前栏目高亮突出显示
var myNav = document.getElementById(“nav”).getElementsByTagName(“a”);
for(var i=0;i<myNav.length;i++){
var links = myNav[i].getAttribute(“href”);
var myURL = document.location.href;
if(myURL.indexOf(links) != -1){
myNav[i].parentNode.className=”selected”;
}
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注