使用 $_GET 方法忽略 css 活动类的 PHP 导航



我的悬停链接和导航一样有效,但活动链接不会出现。

我的 CSS 处于活动状态:

#header ul li a.active,
#header ul li a.active:hover {
    color:#fff;
    background:#000;
    font-weight:bold;

这是我的导航.php文件:

<ul>
<?php
$pages = array(
    "index.php?p=home" => "Home", 
    "index.php?p=contact" => "Contact Us", 
    "index.php?p=services" => "Services", 
    "index.php?p=employees" => "Employees", 
    "index.php?p=dashboard" => "Dashboard");
$p = (isset($_GET['p'])) ? $_GET['p'] : "";
foreach ($pages as $url => $label) {
  echo '<li ';
  if (preg_match("/$p/",$url)) {  
        echo "class='active'";                              
   }                                                      
  echo '><a href=', "$url", '>', "$label", '</a></li>';
}
?>
</ul>

这是我在索引页中检索每个页面的方法:

<?php
  if (file_exists("pages/$p.php")) {
    include("pages/$p.php");
}else{
include("pages/home.php");
}
?>

我想你正在使用这样的网址:/index.php?p=page_name

<ul>
<?php
$pages = array(
    "home" => "Home", 
    "contact" => "Contact Us", 
    "services" => "Services", 
    "employees" => "Employees", 
    "dashboard" => "Dashboard");
$p = (isset($_GET['p'])) ? $_GET['p'] : "";
foreach ($pages as $url => $label) {
  echo '<li ';
  if ($p == $url) {  
        echo "class='active'";                              
   }                                                      
  echo '><a href="index.php?p=' . $url . '">' . $label . '</a></li>';
}
?>
</ul>

相关内容

  • 没有找到相关文章

最新更新