简体   繁体   English

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

[英]How do I use the “active” class correctly in correlation with 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. 我在使用php来控制导航链接是否根据$ CURRENT_PAGE变量设置为“活动”类时遇到问题。

Here is the navigation.php file that is included into every website page file. 这是每个网站页面文件中都包含的navigation.php文件。

 <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: 这是设置$ CURRENT_PAGE变量的配置文件:

<?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 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

只需从案例中删除/ php-template /

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

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