引导4上的Navbar下拉菜单

时间:2018-10-18 17:27:22

标签: jquery html bootstrap-4 navbar

我在Bootstrap 4上的下拉菜单有问题。

这是我的代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>


<nav class="navbar fixed-top navbar-dark bg-primary py-1">
    <a class="navbar-brand" href="#">
        BRAND
    </a>
    <span class="navbar-text py-0">
      <ul class="navbar-nav mr-auto">
          <li class="nav-item dropdown">
              <a class="nav-item nav-link dropdown-toggle" id="userMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  <img src="<?= $objUser->photo ?>" width="40px" height="40px" class="rounded-circle">
                  <span class="caret"></span>
              </a>
              <div class="dropdown-menu dropdown-menu-right" aria-labelledby="userMenu">
                Hi Name
                <a class="dropdown-item" id="logout_btn" href="#">Logout</a>
              </div>
          </li>
      </ul>
    </span>
</nav>

当我单击右侧的链接时,导航栏将展开,以弹出下拉菜单。 并且该链接不可读。

您有什么解决办法吗?

2 个答案:

答案 0 :(得分:2)

您认为我缺少一些下拉菜单,下面的代码应该可以工作

在这里https://jsbin.com/jayukijavi/edit?html,output

<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
  <a class="navbar-brand" href="#">BRAND</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav ml-auto">

      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="nav-item nav-link dropdown-toggle" id="userMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
             <img src="<?= $objUser->photo ?>" width="40px" height="40px" class="rounded-circle">
             <span class="caret"></span>
          </a>
          <a class="dropdown-item" id="logout_btn" href="#">Logout</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

答案 1 :(得分:0)

这里的问题是.navbar .dropdown-menuposition: static;,来自 navbar.css

使用更具体的选择器覆盖CSS时:.navbar .dropdown-menu.dropdown-menu。并将positionstatic更改为absolute即可正常工作。

对于“注销”功能的颜色,您可以执行以下操作来覆盖默认样式:

.navbar-nav .dropdown-item.dropdown-item,
.navbar-nav .dropdown-item.dropdown-item:hover {
  color: blue;
}

.navbar-nav .dropdown-menu.dropdown-menu {
  position: absolute;
}

.navbar-nav .dropdown-item.dropdown-item,
.navbar-nav .dropdown-item.dropdown-item:hover {
  color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>


<nav class="navbar fixed-top navbar-dark bg-primary py-1">
  <a class="navbar-brand" href="#">
        BRAND
    </a>
  <span class="navbar-text py-0">
      <ul class="navbar-nav mr-auto">
          <li class="nav-item dropdown">
              <a class="nav-item nav-link dropdown-toggle" id="userMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  <img src="<?= $objUser->photo ?>" width="40px" height="40px" class="rounded-circle">
                  <span class="caret"></span>
  </a>
  <div class="dropdown-menu dropdown-menu-right" aria-labelledby="userMenu">
    Hi Name
    <a class="dropdown-item" id="logout_btn" href="#">Logout</a>
  </div>
  </li>
  </ul>
  </span>
</nav>