简体   繁体   English

向当前元素添加活动类

[英]Adding active class to current element

I'm looking to add an active element class to my current navbar, but currently struggling. 我正在向当前的导航栏添加一个活动的元素类,但是目前很挣扎。

I've tried the w3schools method, but maybe i've implemented it incorrectly. 我尝试了w3schools方法,但也许我没有正确实现它。

CODE: 码:

 // Get the container element var btnContainer = document.getElementsByClassName("tab"); // Get all buttons with class="btn" inside the container var btns = btnContainer.getElementsByClassName("link"); // Loop through the buttons and add the active class to the current/clicked button for (var i = 0; i < btns.length; i++) { btns[i].addEventListener("click", function() { var current = document.getElementsByClassName("active"); current[0].className = current[0].className.replace(" active", ""); this.className += " active"; }); } 
 #navbar { position: absolute; text-align: right; top: 3.5em; right: 3.5em; z-index: 2; } .tab { background-color: white; opacity: 0.3; height: 3.5em; width: 0.2em; margin-bottom: 1em; } .tab:hover{ opacity:1; transition:0.7s ease; } .link:hover > .text { opacity:1; transition:0.7s ease; } .active, .tab:hover { opacity:1; transition:0.7s ease; } .active, .text:hover > .text { opacity:1; transition:0.7s ease; } 
 <div id="navbar"> <div class="tab"> <a class="link active" href="#home"> <div class="text">Home</div> </a></div> <div class="tab"> <a class="link" href="#work"> <div class="text">Work</div> </a></div> <div class="tab"> <a class="link" href="#about"> <div class="text">About</div> </a></div> </div> 

The nav bar is currently working but theres no active element. 导航栏当前正在工作,但是没有活动元素。 I'd like the tab to be opacity: 1 when active. 我希望标签页不透明:1处于活动状态。

First off, you need to add the class to the parent div, not the link, since it is the parent who has the opacity set to 0.3. 首先,您需要将类添加到父div,而不是链接,因为将父级的不透明度设置为0.3。 Now, I did it in jQuery since it is much easier to achieve. 现在,我在jQuery完成了它,因为它更容易实现。 Hope it is not a problem. 希望这不是问题。

 $('.link').on('click', function() { $('.link').parent().removeClass('active'); $(this).parent().addClass('active'); }); 
 #navbar { position: absolute; text-align: right; top: 3.5em; right: 3.5em; z-index: 2; } .tab { background-color: white; opacity: 0.3; height: 3.5em; width: 0.2em; margin-bottom: 1em; } .tab:hover{ opacity:1; transition:0.7s ease; } .link:hover > .text { opacity:1; transition:0.7s ease; } .active, .tab:hover { opacity:1; transition:0.7s ease; } .active, .text:hover > .text { opacity:1; transition:0.7s ease; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="navbar"> <div class="tab active"> <a class="link" href="#home"> <div class="text">Home</div> </a></div> <div class="tab"> <a class="link" href="#work"> <div class="text">Work</div> </a></div> <div class="tab"> <a class="link" href="#about"> <div class="text">About</div> </a></div> </div> 

See a quick jQuery solution below using the following methods: 使用以下方法查看下面的快速jQuery解决方案:

  • .on() 。上()
  • .closest() .closest()
  • .addClass() and .addClass()和
  • .removeClass() .removeClass()

 $(function() { var links = $('.tab > .link'); links.on('click', function() { links.removeClass('active').closest('.tab').removeClass('active'); $(this).addClass('active').closest('.tab').addClass('active'); }) .first().click(); }); 
 #navbar { position: absolute; text-align: right; top: 3.5em; right: 3.5em; z-index: 2; } .tab { background-color: white; opacity: 0.3; height: 3.5em; width: 0.2em; margin-bottom: 1em; } .tab:hover{ opacity:1; transition:0.7s ease; } .link:hover > .text { opacity:1; transition:0.7s ease; } .active, .tab:hover { opacity:1; transition:0.7s ease; } .active, .text:hover > .text { opacity:1; transition:0.7s ease; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="navbar"> <div class="tab"> <a class="link active" href="#home"> <div class="text">Home</div> </a></div> <div class="tab"> <a class="link" href="#work"> <div class="text">Work</div> </a></div> <div class="tab"> <a class="link" href="#about"> <div class="text">About</div> </a></div> </div> 

In Javascript you can use .querySelectorAll() : 在Javascript中,您可以使用.querySelectorAll()

 document.querySelectorAll('.tab .link').forEach(function(ele, idx) { ele.addEventListener("click", function(e) { e.preventDefault(); document.querySelectorAll('.tab.active').forEach(ele => ele.classList.remove('active')); ele.parentNode.classList.toggle('active'); }); }); 
 #navbar { position: absolute; text-align: right; top: 3.5em; right: 3.5em; z-index: 2; } .tab { background-color: white; opacity: 0.3; height: 3.5em; width: 0.2em; margin-bottom: 1em; } .tab:hover{ opacity:1; transition:0.7s ease; } .link:hover > .text { opacity:1; transition:0.7s ease; } .active, .tab:hover { opacity:1; transition:0.7s ease; } .active, .text:hover > .text { opacity:1; transition:0.7s ease; } 
 <div id="navbar"> <div class="tab"> <a class="link active" href="#home"> <div class="text">Home</div> </a></div> <div class="tab"> <a class="link" href="#work"> <div class="text">Work</div> </a></div> <div class="tab"> <a class="link" href="#about"> <div class="text">About</div> </a></div> </div> 

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

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