PHP 导航高亮是指在网页导航栏中,根据当前页面的 URL 或其他标识,将对应的导航项标记为高亮状态,以便用户能够清晰地识别当前所在页面。
适用于各种需要导航栏的网站,如电商网站、博客、企业官网等。
以下是一个简单的 PHP 导航高亮的实现示例:
<?php
// 假设当前页面的 URL 是 /about.php
$current_page = basename($_SERVER['PHP_SELF']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>导航高亮示例</title>
<style>
.active {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="index.php" <?php if ($current_page == 'index.php') echo 'class="active"'; ?>>首页</a></li>
<li><a href="about.php" <?php if ($current_page == 'about.php') echo 'class="active"'; ?>>关于我们</a></li>
<li><a href="contact.php" <?php if ($current_page == 'contact.php') echo 'class="active"'; ?>>联系我们</a></li>
</ul>
</nav>
<h1>关于我们</h1>
</body>
</html>
原因:
$_SERVER['PHP_SELF']
获取的 URL 不正确。.active
类的样式没有正确应用。解决方法:
$_SERVER['PHP_SELF']
获取的 URL 是正确的。$_SERVER['PHP_SELF']
获取的 URL 是正确的。.active
类的样式正确应用。.active
类的样式正确应用。通过以上内容,你应该能够了解 PHP 导航高亮的基础概念、优势、类型、应用场景以及常见问题的解决方法。
领取专属 10元无门槛券
手把手带您无忧上云