dongwen9975 2013-03-24 08:58
浏览 30
已采纳

如何更改使用php包含的下拉菜单项的类

I have attached my header and navigation to my web pages using php include function. The navigation has an "active" class to indicate which page the user is currently viewing.

I successfully used the following code to achieve this for the regular list items.

<li <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="active"'; ?>><a href="index.php">Home</a></li>

But, I cannot figure out how to achieve this for the pages within a dropdown list item.

<li class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" href="services.php">Services<b class="caret"></b></a>
        <ul class="dropdown-menu">
            <li><a href="services.php">Services</a></li>
            <li><a href="web-development.php">Web Development</a></li>
            <li><a href="graphic-design.php">Graphic Design</a></li>
        </ul>
</li>

The result that I am trying to achieve is, when the user is viewing the "Services", "Web Development", or "Graphic Design" page, the main "Services" list item will have a class= "active".

  • 写回答

1条回答 默认 最新

  • duane2364 2013-03-24 09:11
    关注

    You can achieve this using client-side javascript.

    I recommend using jQuery since is easy to use. Just include it in your html:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    

    And use the addClass() function, for example:

    <script>
    $(function() {
      var url = window.location.href;
      if(url.indexOf("services.php") > -1)
        $('.dropdown-menu [href*=services.php]').addClass('active');
      //repeat the if statement for each case...
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用