简体   繁体   English

如何使用JavaScript(!important)从其他项目处于活动状态时从第一个项目中删除活动类

[英]How to remove the active class from first li-item while other one is active using JavaScript(!important))

I am working on an old site where only JavaScript codes are working. 我在仅JavaScript代码有效的旧站点上工作。 Problem is: the active class is working but while clicking on other "li", the li "Home" still remains active. 问题是:活动类正在运行,但是在单击其他“ li”时,li“ Home”仍然保持活动状态。 Please suggest what other code need to be added in JS. 请提出需要在JS中添加哪些其他代码。 Thanks in advance! 提前致谢! The code are: 代码是:

 function setActive() { aObj = document.getElementById('navmenu').getElementsByTagName('a'); for (i = 0; i < aObj.length; i++) { if (document.location.href.indexOf(aObj[i].href) >= 0) { aObj[i].className = 'active'; } } } window.onload = setActive; 
 <div id="navmenu"> <ul> <li><a href="/">Home</a> </li> <li><a href="/Home/Vision">Vision</a> </li> <li><a href="/Home/Career">Career</a> </li> </ul> </div> 

When you use indexOf() , you're testing whether aObj[i].href is anywhere in document.location.href , you're not testing whether it exactly matches the path. 当使用indexOf() ,您正在测试aObj[i].href是否在document.location.href任何位置,而不是在测试它是否与路径完全匹配。 Since / is in /Home/Vision , that test succeeds. 由于/位于/Home/Vision ,因此测试成功。 Try doing an exact match of document.location.pathname . 尝试完全匹配document.location.pathname

 function setActive() { aObj = document.getElementById('navmenu').getElementsByTagName('a'); for (i = 0; i < aObj.length; i++) { if (document.location.pathname == aObj[i].href) { aObj[i].className = 'active'; } } } window.onload = setActive; 
 <div id="navmenu"> <ul> <li><a href="/">Home</a> </li> <li><a href="/Home/Vision">Vision</a> </li> <li><a href="/Home/Career">Career</a> </li> </ul> </div> 

Check if the below link works with you. 检查以下链接是否适合您。

http://jsfiddle.net/FnK9D/16/ http://jsfiddle.net/FnK9D/16/

function setActive(){aObj = document.getElementById('navmenu').getElementsByTagName('a');
    for (i = 0; i < aObj.length; i++) {
        if (document.location.href.indexOf(aObj[i].href) >= 0) {
            aObj[i].className = 'active';
         }
    }
}

window.onload = setActive;

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

相关问题 如何从多个活动 li 中删除活动类(多选 li) - How to remove class active from multiple active li (multiselect li) 从li切换活动类别,同时从其他li元素中移除活动类别 - Toggling active class from li while removing active from any other li elements 如何从其他部分删除活动 class - How to remove active class from other sections 当我在父项上选择一个项目时,如何在其上添加活动类。 使用javascript - how do i add an active class on the parent li when i select an item on it. using javascript 如何使用Jquery / Javascript添加和删除活动类以在li内锚定标签 - How to add and remove active class to anchor tag within li using Jquery/Javascript 如何从单击的所有li元素中删除活动类? - How to remove active class from all li elements except one that clicked? 如果猫头鹰的第一项处于活动状态,如何在身体上添加类,如果猫头鹰的第一项处于活动状态,则如何删除类? - how to add class on body if owl first item active and remove class if last item is active? 如何从li中删除活动类? - How can I remove the active class from a li? 如何使用Javascript从父级中的所有子级中删除活动类 - How to remove the active class from all children in a parent using Javascript 如何使用Javascript在html的ul li div中设置活动类? - How to set a class active in ul li div in html using Javascript?
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM