jquery 根据网站url给导航nav添加active效果
2024-08-28 17:09:04
后台的同事因为把nav公用了,所以无法单页添加active,一下方法通过判断url的后缀给当前页添加active
$(function(){
var _nava= $('.nav .nav-wrapper a');
var _url = window.location.href;
var _host = window.location.host;
for(var i = 0; i<_nava.length ; i++){
var _astr = _nava.eq(i).attr('href');
if(_url.indexOf(_astr) != -1){
_nava.eq(i).addClass('active').siblings().removeClass('active');
}else if(_url == ('http://'+_host+'/')){
_nava.eq(0).addClass('active').siblings().removeClass('active');
}
}
})
由于后台同事,类别分配不同往常;在点击一个导航进入后,可以正常运行,但是当点击某一条信息进入后,由于网址又是一个新的html,并没有包括在原来的类别内,之后通过网站地图判断
<script type="text/javascript">
$(function(){
var _nava= $('.nav .nav-wrapper a');
var _url = window.location.href;
var _host = window.location.host;
var _type = $('.news .position-search .position-search-wrapper .position a').eq(1).text();//网站地图“首页>新闻类别>新闻详情”
for(var i = 0; i<_nava.length ; i++){
var _atext = _nava.eq(i).text();
var _astr = _nava.eq(i).attr('href');
if(_url.indexOf(_astr) != -1 || _atext == _type){//地图的类别和导航的文本对比,相同在该导航添加active
_nava.eq(i).addClass('active').siblings().removeClass('active');
}else if(_url == ('http://'+_host+'/')){
_nava.eq(0).addClass('active').siblings().removeClass('active');
}
}
})
</script>
最新文章
- MobaXterm.9.4|ssh连接工具
- Win8.1开机黑屏一段时间才能登录
- tornado 路由系统----扩展(include)
- CSS自适应布局(左右固定 中间自适应或者右侧固定 左侧自适应)
- python 3.5.2 install pillow
- js中arguments的用法
- [知识整理]Java集合
- webbench详解
- POJ 1322 Chocolate(母函数)
- Android Dock底座应用开发
- 趣味Python入门(一):初识Python
- Apache Commons Pool2 源码分析 | Apache Commons Pool2 Source Code Analysis
- JavaScript学习笔记:数组reduce()和reduceRight()方法
- 特殊集合(stack、queue、hashtable的示例及练习)
- js求指定时间的周一和周日
- Html5 localStorage 缓存
- 【WebService】WebService基础知识(一)
- Android Touch事件派发流程源码分析
- Function 对象 &; anonymous 匿名函数
- 工作经验:Java 系统记录调用日志,并且记录错误堆栈
热门文章
- (转)AspNetPager查询分页问题(点击页码,不再是查询后的数据集)viewstate解决
- C++深层复制解决指针悬挂
- Jquery函数实现时间显示模式为更新于+被当前时间减去后剩余的时间值(例如:更新于三小时前)的处理。
- [Search Engine] Compression in Inverted Index
- shell编程-1到100的求和与冒泡排序
- 【面试】Spring问答Top 25
- -webkit-appearance: none;
- Lua绑定C++类
- mongodb 基本语法
- 手机QQ v4.2 有感