欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

.active动态使用实现导航效果

程序员文章站 2022-03-17 23:02:38
...

这次给大家带来.active动态使用实现导航效果,.active动态使用实现导航效果的注意事项有哪些,下面就是实战案例,一起来看一下。

通过jq获取你打开页面的链接 window.location.pathname;

在HTML中给自己的li加入一个ID id的命名与网址链接中的href相同

通过jq包含方法找到相对应的li给他加入active类名

然后。。就没有然后了。。。

jq代码:

$(function() {
 varli = $(".title_ul").children("li");
 for(vari = 0; i < li.length; i++) {
 varurl = window.location.pathname;
 varurl = url.replace("/","");
 if(url.indexOf(li[i].id)!=-1) {
  li[i].firstChild.className ="active";
 
 }else{
  li[i].firstChild.className ="";
 }
 }
})

html代码:

<body>
<p class="title">
 <ul class="title_ul">
 <li id="index"><a href="index.html"rel="external nofollow"class="">页面1</a></li>
 <li id="zf"><a href="zf.html"rel="external nofollow"class="">页面2</a></li>
 <li id="gc"><a href="gc.html"rel="external nofollow"class="">页面3</a></li>
 <li id="hc"><a href="hc.html"rel="external nofollow"class="">页面4</a></li>
 <li id="shwt"><a href="shwt.html"rel="external nofollow"class="">页面5</a></li>
 </ul>
</p>
</body>

相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!

推荐阅读:

jQuery做出鼠标滚轮操作图片缩放大小

jQuery实现回到顶部功能

以上就是.active动态使用实现导航效果的详细内容,更多请关注其它相关文章!