简体   繁体   English

根据子元素href属性将活动类设置为元素

[英]set active class to element based on child elements href attribute

I have written code for navigation. 我已经编写了导航代码。 I want to check which class is active, according to that it should set color blue for that respective tab 我想检查哪个类是活动的,根据它应该为相应的选项卡设置蓝色

My Code 我的守则

 $(function() { var pgurl = window.location.href.substr(window.location.href.lastIndexOf("/") + 1); $("#tslcNav ul li a").each(function() { if ($(this).attr("href") == pgurl || $(this).attr("href") == '') $(this).addClass("active"); }) }); 
 .tslcNav li:hover, .tslcNav li.active, .tslcNav li.active { background: none; border-color: #009FD6; color: #009FD6; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <div class="tslcNav" role="navigation"> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li><a href="index.html">Home</a></li> <li><a href="manage.html">Manage </a></li> <li><a href="welcome.html">welcome/a></li> <li><a href="security.html">Security </a></li> </ul> </div> <!--/.nav-collapse --> </div> 

You are placing the active on the a element, not the list Change your javascript code to this: 您将活动放在元素上,而不是列表中将您的javascript代码更改为:

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

Well that is because you are adding class active to anchor element. 那是因为你正在为锚元素添加类active。 And your selector targets li elements with class active. 并且您的选择器将li元素作为活动类。 Hence you need to add class active to parent li, instead of adding it to anchor element. 因此,您需要将类active添加到parent li,而不是将其添加到anchor元素。 you can use .closest() or .parent() here: 你可以在这里使用.closest().parent()

 $(this).closest('li').addClass("active");

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

相关问题 如何根据其子<a>href</a>将活动的CSS类应用于li - How to apply an active css class to li based on its child <a> href javascript:仅在父元素处于活动状态且子元素具有特定类的情况下,才如何向子元素添加属性? - javascript: how to add an attribute to a child element ONLY if a parent element is active AND child element has a certain class? 如何根据元素类将元素elements属性附加到跨度 - How to append element elements attribute to span based on elements class 将属性设置为子元素 - Set attribute to a child element 根据当前页面的元数据设置链接的“活动”类和href属性 - Setting an “active” class and href attribute for link based on the current page's metadata 将活动类应用于href设置为另一个元素的id的导航项目 - Apply active class to nav item whose href is set to id of another element 在一组匹配元素上将父元素添加到父级,并使用Parent Div href - Javascript - Add Child Element to Parent on a Set of Matched Elements, and use Parent Div href - Javascript 如何遍历一组ID并匹配jQuery中另一组元素的活动元素属性? - How can I loop through a set of IDs and match the active element's attribute of another set of elements in jQuery? 向父元素和子元素添加“活动”类 - adding an “active” class to parent and child element 在子状态下向元素添加活动类 - Adding active class to element in child state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM