我只想在使用php打开其页面时在导航链接中添加一个名为“活动”的css类。

在我的网站中,内容分为各个部分。然后使用一个索引文件将这些组件分离,组织和放回原处。 (网站引导文件)实际上,我正在使用php将该网站模块化。

我的导航类似于此代码-

...

<li>
    <a href="index.php?p=dashboard">Dashboard</a>
</li>
<li>
    <a href="index.php?p=page-two">Page Two</a>
</li>
<li>
    <a href="index.php?p=page-three">Page Page Three</a>
</li>

...


这就是我的index.php的样子-

// Validate what page to show:
if (isset($_GET['p'])) {
    $p = $_GET['p'];
} elseif (isset($_POST['p'])) { // Forms
    $p = $_POST['p'];
} else {
    $p = NULL;
}

// Determine what page to display:
switch ($p) {

    case 'dashboard':
        $page = 'dashboard.inc.php';
        $page_title = 'Control Panel';
        break;

    case 'page-three':
        $page = 'page-three.inc.php';
        $page_title = 'Page Three';
        break;

    case 'page-two':
        $page = 'page-two.inc.php';
        $page_title = 'Page Two';
        break;

    // Default is to include the main page.
    default:
        $page = 'login.inc.php';
                $page_title = 'Control Panel Login';
        break;

} // End of main switch.

// Make sure the file exists:
if (!file_exists('./modules/' . $page)) {
    $page = 'login.inc.php';
    $page_title = 'Control Panel Login';
}

include('./includes/header.html');

include('./modules/' . $page);

include('./includes/footer.html');


我不知道如何将active css类动态添加到我的链接。希望有人可以帮助我。

谢谢。

最佳答案

将当前文件与要使用$_SERVER['PHP_SELF']加载的页面进行比较。

如果它们相等:

case 'dashboard':
  $page = 'dashboard.inc.php';
  $page_title = 'Control Panel';
  $class == ($page == $p) ? 'active' : '';
break;


并将此类添加到HTML。

<li class="<?php echo $class;?>">
...

关于php - 在模块化网站中突出显示指向当前页面的链接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27740602/

10-12 12:51
查看更多