dongquxiao8545 2012-05-03 19:25
浏览 34
已采纳

如何使用php + css更改活动菜单的样式

So, I'm writing a menu and I want it to stay a certain color based upon it being on that page. I've added "class = 'active'" onto the page, and I've tried adding it to CSS, but it's not working. Any ideas?

PHP code:

<?php
$currentPage = basename($_SERVER['REQUEST_URI']);
print "<div id = 'submenu-container'>";
print "<div id = 'submenu'>";
print "<ul class = 'about'>";
print "     <li><a  href='about.php#about_intro.php'if($currentPage=='about.php#about_intro.php' || $currentPage=='about.php') echo 'class='active''>About</a></li>";
print "     <li><a href='about.php#about_team.php'if($currentPage=='about.php#about_team.php') echo 'class='active''>Team</a></li>";
print "     <li><a href='about.php#about_services.php' if($currentPage=='about.php#about_services.php') echo 'class='active'>Services</a></li>";            
print " </ul>";
print "</div>";
print"</div>";
?>

CSS:

#submenu ul li a .active {
background:#FFF;    
}
  • 写回答

2条回答 默认 最新

  • doubi5520 2012-05-03 19:29
    关注

    $_SERVER['REQUEST_URI'] will never contain the portion after the hash. It will only show up to about.php and any URL arguments passed with the ?. You should create specific pages for each of these, give the about_intro/about_team/about_services in a query string instead of a hash, or add the active class with javascript.

    Additionally, don't use an if statement when inside of another statement. Use the ternary operator.

    print "<li><a href='about.php'"
        .$currentpage=="about.php"?" class='active'":""
        .">About</a></li>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?