doujie3888 2017-02-20 13:08
浏览 33

如何在点击时激活动态侧边栏

I'm creating dynamic menu. I wanted to highlight the menu once it is selected. We use php. just need to figure out how to append the class active to the current page. Can anyone help me with this?

  <div id="sidebar-wrapper" class="sidebar-toggle">
    <ul class="sidebar-nav">
      <?php  

      $i=1;
    if(count($shipments) > 0) 
      {  
      foreach($shipments as $row) 
        {
        $shipment_id = $row['id'];
        $containerdest = $row['no']."-".$row['destination'];

          ?>
        <li id="shipment<?php echo $shipment_id; ?>"><a href="<?php echo site_url("users/get_details/$shipment_id");?>" id="display<?php echo $shipment_id; ?>"><?php echo $containerdest; ?></a>
        </li>
        <?php $i++; 
        } 
      } ?>


        <li>
            <a href="<?php echo base_url();?>index.php/users/logout">Logout</a>
        </li>
    </ul>
  </div>
  </div>
  • 写回答

1条回答 默认 最新

  • dsjq62428 2017-02-20 13:24
    关注

    Change your html line with below line

    <li id="shipment<?php echo $shipment_id; ?>" class="menu"><a href="<?php echo ite_url("users/get_details/$shipment_id");?>" shipmentid="<?php echo $shipment_id; ?>" ><?php echo $containerdest; ?></a>
    

    And write javascript code as below (assuming you have included jquery library file)

    <script>
     $(document).ready(function(){
           var url = location.pathname;
           var arr = mystr.split("/"); 
           var id = arr[2];
           $(#shipment'+id).attr('shipmentid');
           $('.menu').removeClass('active'); //this will remove all active class after page loading
           $('#shipment'+id).addClass('active'); // this will append active class to the current clicked menu
           });   
    </script>
    
    评论

报告相同问题?

悬赏问题

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