简体   繁体   English

尝试将活动类添加到菜单项

[英]Trying to add active class to menu item

I've spent 2 hours trying to create an active menu with no success, so I figured I would ask for help. 我花了2个小时试图创建一个没有成功的活动菜单,所以我想寻求帮助。 I have an html menu, some js & css... 我有一个html菜单,一些js和CSS ...

 $(function() { var pgurl = window.location.href.substr(window.location.href .lastIndexOf("/") + 1); $("#index li a").each(function() { if ($(this).attr("href") == pgurl || $(this).attr("href") == '') $(this).addClass("active"); }) }); 
 .active { font-weight:bold;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='index'> <div class='container'> <div class='top'> <h1><a href='/' title='The Maths Project'>The Maths Project</a></h1> </div> <ul class='section active_section' id='section_2'> <li><span id='section_title_2' class='section_title'><a href='#' id='section_link_2'>Against the odds.</a></span> <ul> <li id='exhibit_106' class='exhibit_title'><a href="../against-the-odds/introduction"> &rarr; Introduction</a> </li> <li id='exhibit_83' class='exhibit_title'><a href='../against-the-odds/deriving-functions'> &rarr; Deriving functions</a> </li> <li id='exhibit_83' class='exhibit_title'><a href='../against-the-odds/exploiting-odds'> &rarr; Exploiting odds</a> </li> </ul> </li> </ul> <ul class='section' id='section_3'> <li><span id='section_title_3' class='section_title'><a href='http://themathsproject.co.uk' id='section_link_3'>Remembering everything.</a></span> <ul> <li id='exhibit_104' class='exhibit_title'><a href='../against-the-odds/black-swans'>black swans</a> </li> <li id='exhibit_72' class='exhibit_title'><a href='../against-the-odds/in-here-it-is-yesterday'>in here it is yesterday </a> </li> </ul> </li> </ul> </div> </div> 

When the user is on a specific page, the "a" link which they have clicked should become bold. 当用户在特定页面上时,他们单击的“ a”链接应变为粗体。 However, for whatever reason it's not working. 但是,无论出于何种原因,它都不起作用。

I would greatly appreciate any help, 我将不胜感激,

Regards, 问候,

Jack 插口

I am not clear with your question but do you want set active class to menu, so you try the below code 我不清楚您的问题,但您是否要将活动类设置为菜单,因此请尝试以下代码

 $('#navlist a').click(function(e) { e.preventDefault(); //prevent the link from being followed $('#navlist a').removeClass('selected'); $(this).addClass('selected'); }); 
 .nav { color: green; } .selected { color: red; } .san ul li{ float:left; margin-right: 25px; } 
 <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script> <div class="san"> <ul id="navlist"> <li><a class="nav" href="">Home</a></li> <li><a class="nav" href="">About Us</a></li> <li><a class="nav" href="">Services</a></li> <li><a class="nav" href="">Contact</a></li> </ul> </div> 

Try following and check if it works. 尝试按照以下步骤检查是否可行。

$(function() {
    var pgurl = window.location.href.substr(window.location.href.lastIndexOf("/") + 1);
    $("#index li a").each(function() {
        var href = "";
        if($(this).attr("href") != '')
            href = $(this).attr("href").substr(($(this).attr("href").lastIndexOf("/")+1);
            if (href == pgurl || href  == '')
                  $(this).addClass("active");
      })
  });

This line gets the page name: 此行获取页面名称:

var pgurl = window.location.href.substr(window.location.href
                  .lastIndexOf("/") + 1);

you need to apply the same logic to each of your links to see if they match: 您需要对每个链接应用相同的逻辑,以查看它们是否匹配:

$("#index li a").each(function() {

  var aurl = $(this).attr("href").substr($(this).attr("href")
                    .lastIndexOf("/")+1);

  if (aurl == pgurl || aurl == '')
    $(this).addClass("active");
})

Snippet below (tweaked to match as location.href won't match) 以下代码段(已调整为与location.href匹配)

 $(function() { //var pgurl = window.location.href.substr(window.location.href // .lastIndexOf("/") + 1); var locationhref = "mysite.com/against-the-odds/introduction" var pgurl = locationhref.substr(locationhref.lastIndexOf("/")+1); $("#index li a").each(function() { var aurl = $(this).attr("href").substr($(this).attr("href") .lastIndexOf("/")+1); //console.log(aurl) if (aurl == pgurl || aurl == '') $(this).addClass("active"); }) }); 
 .active { font-weight:bold;color:yellow;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='index'> <div class='container'> <div class='top'> <h1><a href='/' title='The Maths Project'>The Maths Project</a></h1> </div> <ul class='section active_section' id='section_2'> <li><span id='section_title_2' class='section_title'><a href='#' id='section_link_2'>Against the odds.</a></span> <ul> <li id='exhibit_106' class='exhibit_title'><a href="../against-the-odds/introduction"> &rarr; Introduction</a> </li> <li id='exhibit_83' class='exhibit_title'><a href='../against-the-odds/deriving-functions'> &rarr; Deriving functions</a> </li> <li id='exhibit_83' class='exhibit_title'><a href='../against-the-odds/exploiting-odds'> &rarr; Exploiting odds</a> </li> </ul> </li> </ul> <ul class='section' id='section_3'> <li><span id='section_title_3' class='section_title'><a href='http://themathsproject.co.uk' id='section_link_3'>Remembering everything.</a></span> <ul> <li id='exhibit_104' class='exhibit_title'><a href='../against-the-odds/black-swans'>black swans</a> </li> <li id='exhibit_72' class='exhibit_title'><a href='../against-the-odds/in-here-it-is-yesterday'>in here it is yesterday </a> </li> </ul> </li> </ul> </div> </div> 

Just traverse the whole links and when any link will clicked then change the font-weight:normal to all links and then apply the font-weight:bold to currently clicked link. 只需遍历整个链接,然后单击任何链接,然后将font-weight:normal更改为所有链接,然后将font-weight:bold应用于当前单击的链接。

$().ready(function () {
        $("ul>li").click(function () {
            $("ul>li").css("font-weight", "normal");
            $(this).css("font-weight", "bold");
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM