dongqiao3927 2018-10-20 00:37
浏览 29
已采纳

如何正确使用“活动”类与php相关联

I'm having issues with the use of php to control if a navigation link is given the class "active" depending on what the $CURRENT_PAGE variable is set to.

Here is the navigation.php file that is included into every website page file.

 <div class="container mt-3">
        <ul class="nav nav-tabs">
          <li class="nav-item">
            <a class="nav-link <?php if ($CURRENT_PAGE == "Index") {?>active<?php }?>" href="index.php">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link <?php if ($CURRENT_PAGE == "About") {?>active<?php }?>" href="about.php">About Us</a>
          </li>
          <li class="nav-item">
            <a class="nav-link <?php if ($CURRENT_PAGE == "Contact") {?>active<?php }?>" href="contact.php">Contact</a>
          </li>
        </ul>
</div>

Here is the config file that sets the $CURRENT_PAGE variable:

<?php
        switch ($_SERVER["SCRIPT_NAME"]) {
                case "/php-template/about.php":
                        $CURRENT_PAGE = "About"; 
                        $PAGE_TITLE = "About Us";
                        break;
                case "/php-template/contact.php":
                        $CURRENT_PAGE = "Contact"; 
                        $PAGE_TITLE = "Contact Us";
                        break;
                default:
                        $CURRENT_PAGE = "Index";
                        $PAGE_TITLE = "Welcome to my homepage!";
        }
?>

For reference of my template, I am using the source code template from this post. https://medium.com/@stevesohcot/sample-basic-php-template-for-file-structure-with-example-code-47ff6d610191

Regarding the problem I am having, the active class is only showing up on the home link, even when I am on a different page such as the contact page. When I hover over the nav links 'about', and 'contact', the active class initiates, but only initiates when I hover over either of the 'about' and 'contact' nav links. Maybe this has to do with the bootstrap classes I am using?

Example

  • 写回答

1条回答 默认 最新

  • doushi3322 2018-10-20 03:29
    关注

    Just remove /php-template/ from case

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错