(轻微打嗝)使用jquery基于URL将活动导航类添加到菜单项 [英] (Minor hiccup) add active navigation class to menu item based on URL using jquery

查看:82
本文介绍了(轻微打嗝)使用jquery基于URL将活动导航类添加到菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法做到了,但得到了一些轻微的打嗝。目前,活动类已添加到当前URL,包括All Watches链接(可能因为URL是主页URL)。



我可以知道我需要什么来创作使脚本完全像当前URL一样工作。



这是HTML代码。



I managed to make it but got some minor hiccup. Currently, the active class been added to current URL including the All Watches link (maybe because the URL is the homepage URL).

May I know what I need to author for the script to work exactly as what the current URL looks like.

This is the HTML codes.

<div class="tagcloud">
<ul class="tag-widget">
    <li>
    <a href="http://effectiveadvisory.com/wingwah-may/">All Watches</a>
    </li>
    <li>
    <a href="http://effectiveadvisory.com/wingwah-may/product-tag/military/">Military</a>
    </li>
    <li>
    <a href="http://effectiveadvisory.com/wingwah-may/product-tag/diver/">Diver</a>
    </li>
    <li>
    <a href="http://effectiveadvisory.com/wingwah-may/product-tag/pilots/">Pilots</a>
    </li>
    <li>
    <a href="http://effectiveadvisory.com/wingwah-may/product-tag/racing/">Racing</a>
    </li>

</ul>

</div>





这是JQuery代码。





This is the JQuery codes.

jQuery(document).ready(function($){
$("#custom_html-2 a")
.filter(function(){ 
return location.href.match($(this).attr("href"))
})
.addClass("tag-active");
});





我尝试过:



在stackoverflow上搜索并发布了一些Google搜索帮助。但没有回应。



我认为问题在于过滤功能。我想我需要使当前的URL与href链接完全匹配。但是我不知道怎么做。



What I have tried:

Some googling and posted help at stackoverflow. But no response.

I think the problem lies in the filter function. I think I need to make the current URL match exactly as the href link. But I've no idea how.

推荐答案

){


(#custom_html-2 a)
.filter(function(){
return location.href.match(
("#custom_html-2 a") .filter(function(){ return location.href.match(


(this).attr(href))
})
.addClass(tag-active);
});
(this).attr("href")) }) .addClass("tag-active"); });





我尝试过:



有些谷歌搜索并在stackoverflow上发布了帮助。但没有回应。



我认为问题在于过滤功能。我想我需要使当前的URL与href链接完全匹配。但我不知道怎么做。



What I have tried:

Some googling and posted help at stackoverflow. But no response.

I think the problem lies in the filter function. I think I need to make the current URL match exactly as the href link. But I've no idea how.


这篇关于(轻微打嗝)使用jquery基于URL将活动导航类添加到菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆