首页 技术 正文
技术 2022年11月15日
0 收藏 842 点赞 4,368 浏览 1733 个字

最近做了一个网站,需要设置导航栏的act属性,这里需要用到addClass以及removeClass:

$('#topName li').removeClass('active');
$(this).addClass('active');

  但是无论怎么都没成功,后来又找到一个需要进行地址匹配一下,忘记看的哪个了,跟下面这个方法一样,就直接粘过来了(这里还有地址~~):

$(document).ready(function(){
$(".navUl li a").each(function(){
$this = $(this);
if($this[0].href==String(window.location)){ //就是这里,需要取跳转页面后的地址,但是我的地址中只要是中文就一直出现乱码
$(".navUl li").removeClass("active");
$this.parent().addClass("active"); //active表示被选中效果的类名
}
});
});

原文:https://blog.csdn.net/weixin_42903350/article/details/82842081

  为了乱码,我捣鼓了一段时间,然后找到了转码的方法:

 obj[decodeURIComponent(tmp_arr[0])] = decodeURIComponent(tmp_arr[1]);

  使用decodeURIComponent()就可以了,嗯,取地址好多方法,我看的这篇—》 直接附别人的文章https://www.cnblogs.com/cang12138/p/7677239.html

//获取当前窗口的路径
var pathname = window.location.pathname;
//结果:/Home/Index//获取参数
var search = window.location.search;
//结果:?id=2

  我需要用到pathname并上search取出的地址,再进行转码,下面是我的代码:

<script type="text/javascript">
$(function () {
$("#topName").find("li").each(function () {
var a = $(this).find("a:first")[0];
var searchUrl = decodeURI(window.location.search); //自动取当前页面的地址,这里主要是对NewsType的值进行解码
var pathUrl = window.location.pathname + searchUrl;
if ($(a).attr("href") === pathUrl)
{
$(this).addClass("act");
} else {
$(this).removeClass("act");
}
});
})
</script>
<div class="container" >
<ul id="topName">
<li class="act"><a href="/PCSite/News/NewsList?NewsType=全部" rel="external nofollow" target="_top">全部</a></li>
<li><a href="/PCSite/News/NewsList?NewsType=公告1" rel="external nofollow" target="_top">公告1</a></li>
<li><a href="/PCSite/News/NewsList?NewsType=公告2" rel="external nofollow" target="_top">公告2</a></li>
<li><a href="/PCSite/News/NewsList?NewsType=公告3" rel="external nofollow" target="_top">公告3</a></li>
<li><a href="/PCSite/News/NewsList?NewsType=公告4" rel="external nofollow" target="_top">公告4</a></li>
<li><a href="/PCSite/News/NewsList?NewsType=公告5" rel="external nofollow" target="_top">公告5</a></li>
</ul>
</div>

 这样就能实现在页面刷新后动态添加act的功能。

ps:如有侵权,请联系,立马删!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860