dongmanni6916 2012-09-20 13:55
浏览 192
已采纳

当class = class =“active”时,将class =“current”添加到父菜单

I apparently have no idea of how to add class="current" to the <li> tag of the parent menu when it's <li> tag has a class="active". I tried using jQuery to implement this, but It does not work since the page is reloaded when clicking on it. So I simply added a field called name into the database to use as a parameter. Bellowing is my PHP code:

echo '<ul>';
    $main_menu = model::getMainMenu();
       foreach($main_menus as $menu){
            echo '<li class="dropdown">
            <a href=?page='.$menu['name'].'&m=1 class='.(($_GET['page'] == $menu['name'])?'current':'').'>'.$menu['title'].'</a>';
            $sub_menus = model::getSubMenu($menu['id']);
            if( $sub_menus != '' ){
              echo '<ul>';
              foreach($sub_menus as $sub){
                echo '<li><a href=?page='.$sub['name'].' class='.(($_GET['page']==$sub['name'])?'current':'').'>'.ucwords( $sub['title'] ).'</a></li>';
              }
             echo '</ul>';
            }
            echo '</li>';
    }
</ul>

I can only detect which sub page or page is currently active by comparing the $_GET['page'] and the $sub['name'] (my url structure is simply like this index.php?page=home). However, I have no idea how to detect the parent menu when its child is currently selected.

I uploaded the page here

  • 写回答

2条回答 默认 最新

  • duanfang5849 2012-09-20 14:07
    关注

    See code below. Not sure if it is completely correct, but idea should be clear from it. Basically - you can collect all LIs html into a variable and set a flag to tru if one of LIs is active. And than echo everything taking into account flag variable state.

    echo '<ul>';
        $main_menu = model::getMainMenu();
           foreach($main_menus as $menu) {                
                $sub_menus = model::getSubMenu($menu['id']);
                $lis = "";
                $hasCurrent = false;
                if($sub_menus != '') {
                    foreach($sub_menus as $sub) {
                        $lis .= '<li><a href=?page='.$sub['name'].' class='.(($_GET['page']==$sub['name'])?'current':'').'>'.ucwords( $sub['title'] ).'</a></li>';
                            if($_GET['page']==$sub['name']) {
                                $hasCurrent = true;
                            }
                    }
                    echo '<li class="dropdown ' . $hasCurrent ? 'active' : '' . '">
                          <a href=?page='.$menu['name'].'&m=1 class='.(($_GET['page'] == $menu['name'])?'current':'').'>'.$menu['title'].'</a>';
                    if($lis != '') {
                        echo '<ul>';
                        echo $lis;
                        echo '</ul>';
                    }
                }
                echo '</li>';
        }
    </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问Ubuntu要怎么安装chrome呀?
  • ¥15 视频编码 十六进制问题
  • ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊