简体   繁体   English

页面处于活动状态时更改类

[英]Change class when page is active

How can I set a .html page active when I change pages? 如何在更改页面时将.html页面设置为活动状态? This is needed to change the active tab in my nav bar. 这是更改导航栏中的活动选项卡所必需的。 I should add the class .current_page_item to the active li of the nav. 我应该将类.current_page_item添加到nav的活动li中。

I've searched a bit around, but I couldn't find a fitting answer. 我搜索了一下,但我找不到合适的答案。

this is what you need with jquery 这就是jquery所需要的

   <script>
   $(document).ready(function(){

       var links = $('nav').children();
       $.each(links, function(key, value){
          if(value.href == document.URL){
           $(this).addClass('current_page_item');
          }
       });
   </script>
    <nav>
        <a href="/link1">link-1</a>
        <a href="/link2">link-2</a>
        <a href="/link3">link-3</a>
        <a href="/link4">link-4</a>
        <a href="index.html">link-5</a>
    </nav>

Link 5 will get the active class because its the same as the current url 链接5将获得活动类,因为它与当前URL相同


The Result 结果

<nav>
    <a href="/link1">link-1</a>
    <a href="/link2">link-2</a>
    <a href="/link3">link-3</a>
    <a href="/link4">link-4</a>
    <a class="current_page_item" href="index.html">link-5</a>
</nav>

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

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