简体   繁体   English

将活动 CSS class 添加到菜单项

[英]Add active CSS class to menu item

I have this category menu.我有这个类别菜单。 When I click on a category menu item, it opens the category page and what I need it to add the 'active' Bootstrap class on the menu item that was selected (clicked).当我单击一个类别菜单项时,它会打开类别页面以及我需要它在选择(单击)的菜单项上添加“活动”引导 class 的内容。

How can I do it?我该怎么做?

<?php      
 $cat = "SELECT * FROM category WHERE cat_parent_id = :value ORDER BY cat_id ASC"; 
   $stmt = $con->prepare($cat);
   $stmt->bindValue(':value', 1, PDO::PARAM_STR);
      $stmt->execute(); 
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
          extract($row);
             $name_cat = str_replace("-"," ", $cat_name);
             $name_under = str_replace(" ","-", $cat_name);
             ?>     
               <li><a class="dropdown-item" href="<?php echo $home_url . 
                $cat_id."/".strtolower($name_under);?>.html"><?php echo strtoupper($name_cat); ?></a> 
                 </li>
<?php } ?>  

Going an extra step on the comment above from @El_Vanja:从@El_Vanja 对上述评论采取额外措施:

Figure out the current url with the following code:使用以下代码找出当前的 url:

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Then add that condition when you're forming the link:然后在形成链接时添加该条件:

<li<?php if($home_url == $actual_link echo ' class="active"';?>><a class="dropdown-item" href="<?php echo $home_url . 
            $cat_id."/".strtolower($name_under);?>.html">
            <?php echo strtoupper($name_cat); ?></a></li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM