jQuery使链接处于活动状态 [英] jQuery Make a Link in Active Status

查看:91
本文介绍了jQuery使链接处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理HTML模板,但是无法触摸HTML代码,只能处理CSS和JS文件.因此,我无法以任何方式编辑HTML代码.

I am working on a HTML template, but I cannot touch the HTML code, I can only work on CSS and JS files. So I cannot in any way edit the HTML code.

我要实现的目的是,当jQuery或Javascript识别出当前页面URL与我要置于活动状态的链接相同时,将某些链接置于活动状态,而无需编辑HTML代码.

What I am trying to achieve is to put some links in active status when jQuery or Javascript recognizes that the current page URL is the same one of the link I want to put in active status, without editing the HTML code.

有办法吗?我尝试了很多方法,但是没有运气.

Is there a way to do it? I tried in many ways but with no luck.

这是HTML代码(请记住,我无法对其进行编辑).

Here is the HTML code ( Remember I cannot edit it ).

<span class="Tag_Nav_Aux">
    <a href="/my-account/create-account.aspx">Create&nbsp;Account</a>
    &nbsp;|&nbsp;
    <a href="/my-account/login.aspx">Login</a>
    &nbsp;|&nbsp;
    <a href="/my-cart/default.aspx">My Cart</a>
</span>

由于我在用户登录或注销时HTML发生了更改,因此jQuery或Javascript代码应在除我上面报告的链接之外的其他链接上起作用. 因此,jQuery应该指向类Tag_Nav_Aux,并将Active类添加到任何a标签,它将发现该链接具有与当前URL相同的链接.

The jQuery or Javascript code should work on different links, other than the ones I reported above, since the HTML changes when the user is logged in or logged out. So the jQuery should point the class Tag_Nav_Aux and add the Active class to any a tag it will find that has the link the same of the current URL.

推荐答案

尝试此脚本

jQuery(function($){
    $('.Tag_Nav_Aux a').filter(function(){
       return $(this).attr('href').toLowerCase() === window.location.pathname.toLowerCase();
    }).addClass('active');
});

并为

.Tag_Nav_Aux a.active{
   // style you want the active link to have
}

这篇关于jQuery使链接处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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