【问题标题】:How do i keep my current page menu highlighted on pagination?如何在分页上保持当前页面菜单突出显示?
【发布时间】:2013-10-14 08:39:41
【问题描述】:

我在一个页面中有一个导航菜单,它通过添加一个类来根据 URL 突出显示您当前所在的菜单。

我的问题是我的页面是分页的,所以有时用户会在:

http://example.com:4001/wordpress/calientes/page/2/ , /page/3/ , /page/4/ 等等...

在用户浏览页面时,让我的菜单突出显示的最佳方式是什么?

这是我的菜单:

<div class="innerhead">

<ul>

<li> <a href="http://example.com:4001/wordpress/calientes/">Calientes </a> </li>
<li><a href="http://example.com:4001/wordpress/tendencias/">Tendencias</a></li>

</ul>

</div>

这是我用来突出显示当前页面菜单的脚本:

<script>
$(function(){
    // this will get the full URL at the address bar
    var url = window.location.href; 

    // passes on every "a" tag 
    $(".innerhead a").each(function() {
            // checks if its the same on the address bar
        if(url == (this.href)) { 
            $(this).closest("li").addClass("active");
        }
    });
});
</script>

【问题讨论】:

    标签: javascript css menu pagination


    【解决方案1】:

    另一种选择可能是:

    <div class="innerhead">
      <ul>
        <li id="calientes"> <a href="http://example.com:4001/wordpress/calientes/">Calientes </a> </li>
        <li id="tendencias"> <a href="http://example.com:4001/wordpress/tendencias/">Tendencias</a></li>
      </ul>
    </div>
    

    在您的脚本中:

    var url = window.location.pathname.split('/');
    $('#'+url[4]).addClass("active");
    

    【讨论】:

    • 你在做这个吗?
    • 如果您不使用 $(document).ready(),请确保在 html 文件末尾包含脚本代码
    • 这个脚本会突出显示我的菜单: 但是当我使用分页时,我的 Active 类停止工作......
    • 问题出在索引上。它不是 4。使用 2。我无法尝试使用您的网址。确保添加到您的脚本 console.log(window.location.pathname.split('/'));它会让你找到正确的索引
    • 好朋友!它就像一个魅力!我将其更改为“2”,它确实有效!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 2018-09-03
    • 2011-06-05
    • 2011-10-27
    • 2016-02-25
    • 1970-01-01
    相关资源
    最近更新 更多