我试图弄清楚如何设置多页面链接的样式。我的index.php中有一个导航栏,可在同一页面上打开其他页面。 我还有另一个导航栏,上面有一些样式:
class="<?= basename($_SERVER['REQUEST_URI']) == "multi_page.php" ? "selected" : ""; ?>" href="multi_page.php">Multisida</a>
这适用于普通链接,但我似乎无法在多页面链接上使用它。我做错了吗?我要设置样式的链接如下所示:
<a href="?page=index">Index</a>
致谢
答案 0 :(得分:0)
我从老师那里得到了答案。我想和你分享。
这是来自multipage.php:
<?php
$pageReference = $_GET["page"] ?? "index";
$multiPages = [
"index" => [
"title" => "Intro",
"subpage" => __DIR__ . "/multipage/index.php",
],
"print-get" => [
"title" => "Print variable",
"subpage" => __DIR__ . "/multipage/print-get.php",
]
];
$newPage = $multiPages[$pageReference] ?? null;
if (!$newPage) {
die("Fel, fel, fel...");
}
$title = "{$newPage['title']}";
$m_subpage = $newPage["subpage"];
include __DIR__ . "/view/header.php";
include __DIR__ . "/view/multi_page.php";
include __DIR__ . "/view/footer.php";
从view / multi_page.php中查看:
<div class="wrap-main">
<h2>Multipage</h2>
<?php include __DIR__ . "/multi_aside.php"; ?>
<article class="subpage">
<?php if ($pageReference) : ?>
<?php require $m_subpage; ?>
<?php else : ?>
<p>You have selected something '<?= htmlentities($pageReference) ?>' that
does not map to an actual page.</p>
<?php endif; ?>
</article>
</div>
来自multipage_aside.php:
<aside>
<ul class="aside">
<?php foreach ($multiPages as $key => $value) : ?>
<?php
$selected = null;
if ($key === $pageReference) {
$selected = "class='selected'";
}
?>
<li><a <?= $selected ?> href="?page=<?= $key ?>"><?= $value["title"] ?></a></li>
<?php endforeach; ?>
</ul>
</aside>
最后是CSS:
.aside .selected {
color: purple;
font-weight: bolder;
text-decoration: underline;
}