var urlstr = location.href; //alert(urlstr); var urlstatus=false; $("#topnav a").each(function () { if ((urlstr + '/#').indexOf($(this).attr('rel')) > -1&&$(this).attr('rel')!='') { $(this).addClass('am-active'); urlstatus = true; } else { $(this).removeClass('am-active'); } }); // alert(urlstatus); if (!urlstatus) {$("#topnav a").eq(0).addClass('am-active'); }
需要说明 首页链接必须为”/#” 不然可能会冲突 导致其他栏目首页高亮不消失
]]>原创文章,作者:键盘游走者,如若转载,请注明出处:https://www.708034.com/2017/12/js-jq-%e5%bd%93%e5%89%8d%e6%a0%8f%e7%9b%ae%e9%ab%98%e4%ba%ae/