doumaqing6652 2016-12-30 14:00
浏览 16

PHP:如何检查哪个导航栏点是活动的?

I have to check which listpoint in the menu is active and I'd like to know if there's a way in php?

<ul class="menu">
  <li class="a">...</li>
  <li class="b">...</li>
  <li class="c">...</li>
</ul>

So how can I check if a is active or b or c, etc. ?

In my example I mean, how can I give the <li> which you selected a class="...", but only this one.

  • 写回答

2条回答 默认 最新

  • dtj88302 2016-12-30 14:17
    关注

    In a pinch you can get this done with php sessions, simply set a variable any time a menu is switched.

    The session variable should be set near the top of whatever page it is being used on, so that way PHP can recognize it before the menu loads on the page.

    Here is a small snippet for inspiration, you could do this a lot of different ways.

    <?
    session_start();
    
    if(isSet($_GET['switch'])) {
        $_SESSION['menuPage'] = $_GET['switch'];
    }
    $menuPage = $_SESSION['menuPage'];
    
    echo $menuPage."<br>";
    
    ?>
    
    <ul class="menu">
        <li <? if(strcmp($menuPage,"a") == 0) { echo "active"; } ?> class="a"><a href="?switch=a">A</a></li>
        <li <? if(strcmp($menuPage,"b") == 0) { echo "active"; } ?> class="b"><a href="?switch=b">B</a></li>
        <li <? if(strcmp($menuPage,"c") == 0) { echo "active"; } ?> class="c"><a href="?switch=c">C</a></li>
    </ul>
    

    Note: You want to use a session and not a normal variable so that the $_GET for the menu doesn't have to constantly be present in the URL.

    评论

报告相同问题?

悬赏问题

  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真