douguio0185 2016-03-04 12:24
浏览 38
已采纳

无法使用PHP [活动]向Bootstrap下拉菜单添加活动类

This question already has an answer here:

Overview

I have a navbar with li elements. I add the class active to the element that corresponds to a variable $page.

For each of the list elements, I place code similar to this:

<li<?= $page=="new" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=new">New event</a></li>

That way, using this example, if the $page is "new", I add the active class to the li element.

This works with top-level list items as well as dropdown children.

Issue

I want the parent element of the dropdown children to also show the active class when one of its children is active.

To do this with a dropdown with page values "new", "existing" and "gateway", I use:

<li class="dropdown<?= $page=="new"||$page=="existing"||$page="gateway" ? " active" : null ?>">

However, this doesn't work; when inspecting the element in the Chrome developer tools, the only class is dropdown1 and any other added classes have disappeared.

Adding the active class manually in Chrome developer tools produces the correct result.

Full code

<div id="navbar" class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <li<?= $page=="dashboard" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=dashboard">Dashboard</a></li>
        <li class="dropdown<?= $page=="new"||$page=="existing"||$page="gateway" ? " active" : null ?>">
            <a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Events <span class="caret"></span></a>
            <ul class="dropdown-menu">
                <li<?= $page=="new" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=new">New event</a></li>
                <li<?= $page=="existing" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=existing">Existing events</a></li>
                <li<?= $page=="gateway" ? " class='active'" : null ?>><a href="<?= $rootUrl ?>?page=gateway">Gateway</a></li>
            </ul>
        </li>
        <li><a href="<?= $rootUrl ?>?logout=true">Sign out</a></li>
    </ul>
</div>

Question

How can I make the parent element of the dropdown have active class when one of its child elements has the active class?

</div>
  • 写回答

1条回答 默认 最新

  • doucang5542 2016-03-04 12:35
    关注

    You are missing a "=" at $page="gateway" should be $page=="gateway"

    <li class="dropdown<?= $page=="new"||$page=="existing"||$page="gateway" ? " active" : null ?>">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用