0

嗨,我试图让我的导航栏按钮处于活动状态,以便在该特定页面上看起来不同。

当我手动将我的 li 类更改为活动状态时,它可以工作,但是当我使用下面的代码执行此操作时,该类保持不变。

<?php 
    echo '<ul class="nav">';

        echo ($PHP_SELF == '/index.php') ?
        '<li class="active"><a href="index.php">Home</a></li>' :
        '<li class="none"><a href="index.php">Home</a></li>';

        echo ($PHP_SELF == '/how-it-works.php') ?
        '<li class="active"><a href="how-it-works.php">How it works</a></li>' :
        '<li class="none"><a href="how-it-works.php">How it works</a></li>';

        echo ($PHP_SELF == '/gas.php') ?
        '<li class="active"><a href="gas.php">Gas</a></li>' :
        '<li class="none>"><a href="gas.php">Gas</a></li>';

        echo ($PHP_SELF == '/electric.php') ?
        '<li class="active"><a href="electric.php">Electric</a></li>' :
        '<li class="none"><a href="electric.php">Electric</a></li>';

        echo ($PHP_SELF == '/telecoms.php') ?
        '<li class="active"><a href="telecoms.php">Telecoms</a></li>' :
        '<li class="none>"><a href="telecoms.php">Telecoms</a></li>';

        echo ($PHP_SELF == '/services.php') ?
        '<li class="active"><a href="services.php">Services</a></li>' :
        '<li class="none"><a href="services.php">Services</a></li>';

        echo ($PHP_SELF == '/contact.php') ?
        '<li class="active"><a href="contact.php">Contact</a></li>' :
        '<li class="none"><a href="contact.php">Contact</a></li>';

         echo '</ul>';
?>

所以我想要的是当我在索引页面上时课程处于活动状态,当我不在索引页面上时我希望课程没有

4

2 回答 2

2

尝试使用 PHP 的服务器变量http://php.net/manual/en/reserved.variables.server.php

$_SERVER['PHP_SELF']

我认为这会起作用

于 2013-03-05T11:38:33.913 回答
0
<?php 
            echo '<ul class="nav">';

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

                echo ($_SERVER['PHP_SELF'] == 'how-it-works.php') ?
                '<li class="active"><a href="how-it-works.php">How it works</a></li>' :
                '<li class="none"><a href="how-it-works.php">How it works</a></li>';

                echo ($_SERVER['PHP_SELF'] == 'gas.php') ?
                '<li class="active"><a href="gas.php">Gas</a></li>' :
                '<li class="none>"><a href="gas.php">Gas</a></li>';

                echo ($_SERVER['PHP_SELF'] == 'electric.php') ?
                '<li class="active"><a href="electric.php">Electric</a></li>' :
                '<li class="none"><a href="electric.php">Electric</a></li>';

                echo ($_SERVER['PHP_SELF'] == 'telecoms.php') ?
                '<li class="active"><a href="telecoms.php">Telecoms</a></li>' :
                '<li class="none>"><a href="telecoms.php">Telecoms</a></li>';

                echo ($_SERVER['PHP_SELF'] == 'services.php') ?
                '<li class="active"><a href="services.php">Services</a></li>' :
                '<li class="none"><a href="services.php">Services</a></li>';

                echo ($_SERVER['PHP_SELF'] == 'contact.php') ?
                '<li class="active"><a href="contact.php">Contact</a></li>' :
                '<li class="none"><a href="contact.php">Contact</a></li>';

            echo '</ul>';
            ?>

删除所有“/”并使用 $_SERVER['PHP_SELF']。

于 2013-03-05T11:47:09.987 回答