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

时间:2017-02-20 13:08:49

标签: javascript php twitter-bootstrap codeigniter modal-dialog

我正在创建动态菜单。我想在选择后突出显示菜单。  我们用php。只需要弄清楚如何将活动类附加到当前页面。任何人都可以帮我这个吗?

  <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 个答案:

答案 0 :(得分:1)

使用以下行更改您的html行

<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>

编写如下的javascript代码(假设你已经包含了jquery库文件)

<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>