【问题标题】:Adding active class to parent and child element when child is active with PHP当子元素使用 PHP 处于活动状态时,将活动类添加到父元素和子元素
【发布时间】:2015-10-06 14:18:15
【问题描述】:

当子元素Subpage1.phpSubpage2.php 处于活动状态时,是否有解决方案,以便父元素practices.php 也获得class="active"

<ul>
    <li class="<?php echo ($_SERVER['PHP_SELF'] == " /index.php " ? "active " : " ");?>">
        <a href="index.php">Start</a>
    </li>
    <li class="<?php echo ($_SERVER['PHP_SELF'] == " /practices.php " ? "active " : " ");?>">
        <a href="practices.php">Practices</a>
        <ul>
            <li class="<?php echo ($_SERVER['PHP_SELF'] == " /subpage1.php " ? "active " : " ");?>">
                <a href="subpage1.php">Subpage1</a>
            </li>
            <li class="<?php echo ($_SERVER['PHP_SELF'] == " /subpage2.php " ? "active " : " ");?>">
                <a href="subpage2.php">Subpage2</a>
            </li>
        </ul>
    </li>
</ul>

A similar question was asked here, but the parent child problem couldn't be solved there

【问题讨论】:

    标签: php html drop-down-menu submenu


    【解决方案1】:

    这不是最佳的,但您可以使用 in_array() 对照元素及其子元素检查 $_SERVER['PHP_SELF'] 的值:

    <li class="<?php echo (in_array($_SERVER['PHP_SELF'], array(
            '/practices.php',
            '/subpage1.php',
            '/subpage2.php',
        )) ? "active " : " ");?>">
        <a href="practices.php">Practices</a>
        ...
    </li>
    

    如果$_SERVER['PHP_SELF'] 匹配该数组中的任何项目,则该类将设置为活动。它可能没有循环那么麻烦。

    【讨论】:

      猜你喜欢
      • 2015-09-12
      • 2017-11-24
      • 2012-04-29
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多