使用jQuery将当前URL与UL中的herf匹配,并在匹配时添加类 [英] Use jQuery to match current URL to herf in a UL and add class if matched

查看:73
本文介绍了使用jQuery将当前URL与UL中的herf匹配,并在匹配时添加类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态创建的UL.我想使用jQuery来使用当前URL,搜索指定的UL并找到具有匹配href的任何li.如果找到一个,则添加"current_page"类.

I have a dynamically created UL. I want to use jQuery to use the current URL, search through a specified UL and find any li with a matching href. If one is found then add the class "current_page".

我想出了这个办法,但似乎并没有解决问题.

I came up with this but it doesn't seem to do the trick.

$(document).ready(function() {  
    $("ul.nav_cat_archiveli").find("a[href='"+window.location.pathname+"']").each(function() {
        $(this).addClass("current_page");
    });
});

推荐答案

您可以玩窗口.location ,它将返回当前页面的URL以及 href 属性window.location的

You can play with window.location which will return the current page URL and also the href property of the window.location

这篇关于使用jQuery将当前URL与UL中的herf匹配,并在匹配时添加类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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