Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何根据URL和URL向锚定标记添加活动类?_Javascript_Jquery_Html_Url_Window.location - Fatal编程技术网

Javascript 如何根据URL和URL向锚定标记添加活动类?

Javascript 如何根据URL和URL向锚定标记添加活动类?,javascript,jquery,html,url,window.location,Javascript,Jquery,Html,Url,Window.location,如果URL为www.mysite.com/home/,则我有这个工作减去部分。如果它是www.mysite.com/home/index.aspx,它就可以工作,但如果你取下index.aspx,它就不能工作。它也适用于我拥有的所有其他页面-page2.aspx、page3.aspx等 此外,我可能会在页面上重写URL(因此page2.aspx将是page2,page3.aspx将是page3) 如何调整下面的代码,以便能够将活动类添加到活动页面 jQuery: $(function() {

如果URL为www.mysite.com/home/,则我有这个工作减去部分。如果它是www.mysite.com/home/index.aspx,它就可以工作,但如果你取下index.aspx,它就不能工作。它也适用于我拥有的所有其他页面-page2.aspx、page3.aspx等

此外,我可能会在页面上重写URL(因此page2.aspx将是page2,page3.aspx将是page3)

如何调整下面的代码,以便能够将活动类添加到活动页面

jQuery:

$(function() {
    var pgurl = window.location.href.substr(window.location.href
    .lastIndexOf("/")+1);
    $("nav a").each(function(){
        if($(this).attr("href") == pgurl || $(this).attr("href") == '')
        $(this).addClass("active");
    })
});
添加类的HTML:

 <nav class="clearfix">
  <a href="index.aspx">Home</a>
  <a href="page2.aspx">Page2</a>
  <a href="page3.aspx">Page3</a>
</nav>

您只需检查
窗口.位置.路径名

$(function() {
    $("nav a").each(function(){
        if($(this).attr("href") == window.location.pathname || window.location.pathname == '')
        $(this).addClass("active");
    })
});
试试这个

var s = window.location.href.substr(window.location.href.lastIndexOf("/")+1);
if(!s)
    document.querySelector("nav > a:first-child").className = "active";
else
    document.querySelector("nav > a[href*='"+s+"']").className = "active";

如果您想找到与正在查找的url部分相同的链接,可以尝试以下操作:

var lookup = window.location.href.substr(window.location.href.lastIndexOf("/")+1);
$("a:regex(href, .*"+ lookup +".*)").addClass('active');

老天爷知道怎么做no索引了!我没想到要得到答案会这么难。还是不确定是否有虚荣心。可能会在这方面单独发表文章

$(document).ready(function() {

// Add active class to menu
$(function() {
    var pgurl = window.location.href.substr(window.location.href
    .lastIndexOf("/")+1);
    $("nav a").each(function(){
        if($(this).attr("href") == pgurl)
            $(this).addClass("active");
        if (pgurl == '')
                $("nav a:first-child").addClass("active");
    })
});
});

我知道,也许这是个愚蠢的解决方案,但是:

  var current_url = window.location.hash.substr(1);
  $('.news #' + current_url).addClass('active');

机器坏了。因此,如果它是www.mysite.com/home/它没有将活动类添加到@rookstrife,我理解错了,请检查editSame问题-不适用于/-因此,如果有人在www.mysite.com/home/中键入活动类,则我假设活动类未添加到bc,则索引页不在URL中。即使正在显示索引页(如虚荣URL)。@rookstrife如果您认为它是正确的,'index.aspx'被服务器隐藏,因此您需要再次检查。查看我的编辑OK,
窗口。位置。路径名是您需要的。只需使用
alert()
玩玩它,就可以处理那些根本不起作用的情况。禁止添加任何活动类。除非我做错了什么。我做错了是明显的-1不等于1现在你可以尝试同样的问题,因为Vlad L-在/-上不起作用-因此,如果有人在www.mysite.com/home/中键入活动类,我假设bc的索引页不在URL中。即使索引页正在显示(就像一个虚荣的URL)。不-同一个问题:]好肉汁这变成了一个问题。我又犯了一个错误,我忘了写比较:/根本不起作用。禁止添加任何活动类。除非我做错了什么。代码不起作用,并且在相应页面上阻止将活动类添加到任何锚标记。