Bootstrap 响应式菜单切换器的问题

Posted

技术标签:

【中文标题】Bootstrap 响应式菜单切换器的问题【英文标题】:Problem with responsive menu toggler with Bootstrap 【发布时间】:2022-01-21 17:03:56 【问题描述】:

我用 Bootstrap 创建了一个导航栏。在大屏幕上似乎一切正常,但在小屏幕上,当我单击切换器图标时,菜单不会折叠。我尝试了多种方法,例如编写“显示:内容;”在css文件中,但没有任何效果。你能告诉我我做错了什么吗?这是我的代码:

  <nav id="navbar" class="navbar navbar-expand-lg navbar-light bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="index.html"><img src="images/logo.png" ></a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
          
          <!-- home -->
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="index.html" style="margin-left: 20px">Home</a>
        </li>
          
        <!--- women --->
<li class="nav-item dropdown">
    <div class="dropdown">
        <a class="nav-link dropdown" id="navbarDropdown" role="button" data-toggle="dropdown" aria-expanded="false">
                  Women
        </a>
      <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
        <a class="dropdown-item" href="#">Clothes</a>
        <a class="dropdown-item" href="#">Accessories</a>
        <a class="dropdown-item" href="#">Shoes</a>
      </div>
    </div>
</li>
          <!--- men --->
<li class="nav-item dropdown">
    <div class="dropdown">
        <a class="nav-link dropdown" id="navbarDropdown" role="button" data-toggle="dropdown" aria-expanded="false">
                  Men
        </a>
      <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
        <a class="dropdown-item" href="#">Clothes</a>
        <a class="dropdown-item" href="#">Accessories</a>
        <a class="dropdown-item" href="#">Shoes</a>
      </div>
    </div>
</li>
          
          <!--- help --->
<li class="nav-item dropdown">
    <div class="dropdown">
        <a class="nav-link dropdown" id="navbarDropdown" role="button" data-toggle="dropdown" aria-expanded="false">
                  Help
        </a>
      <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
        <a class="dropdown-item" href="sizes.html">Sizes</a>
        <a class="dropdown-item" href="faqs.html">FAQs</a>
        <a class="dropdown-item" href="contact-us.html">Contact Us</a>
        <a class="dropdown-item" href="about-us.html">About Us</a>
      </div>
    </div>
</li>
         
          <!-- search box -->
        <form class="form-inline">
        <input class="form-control rounded" type="search" placeholder="What are you looking for?" aria-label="Search" size=60px style="text-align: left;">
        <button type="submit" class="btn btn-outline-dark" style="margin-left: 5px"><i class="bi bi-search"></i></button>
        </form>
          
          <!-- shopping cart -->
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="cart.html">
               <i class="bi bi-bag" style="font-size: 30px; margin-left: 650px;"></i>
              <span class="cart-basket d-flex align-items-center justify-content-center">
            0
          </span>
          </a>
        </li>
          
      </ul>

    </div>
  </div>
</nav>

【问题讨论】:

【参考方案1】:
Add CSS
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
 <body>
 

    <nav id="navbar" class="navbar navbar-expand-lg navbar-light bg-light">
        <div class="container-fluid">
          <a class="navbar-brand" href="index.html"><img src="images/logo.png" ></a>
          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
          <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                
                <!-- home -->
              <li class="nav-item">
                <a class="nav-link active" aria-current="page" href="index.html" style="margin-left: 20px">Home</a>
              </li>
                
              <!--- women --->
      <li class="nav-item dropdown">
          <div class="dropdown">
              <a class="nav-link dropdown-toggle" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                        Women
              </a>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item" href="#">Clothes</a>
              <a class="dropdown-item" href="#">Accessories</a>
              <a class="dropdown-item" href="#">Shoes</a>
            </div>
          </div>
      </li>
                <!--- men --->
      <li class="nav-item dropdown">
          <div class="dropdown">
              <a class="nav-link dropdown-toggle" id="navbarDropdown" role="button"  data-bs-toggle="dropdown" aria-expanded="false">
                        Men
              </a>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item" href="#">Clothes</a>
              <a class="dropdown-item" href="#">Accessories</a>
              <a class="dropdown-item" href="#">Shoes</a>
            </div>
          </div>
      </li>
                
                <!--- help --->
      <li class="nav-item dropdown">
          <div class="dropdown">
              <a class="nav-link dropdown-toggle" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                        Help
              </a>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item" href="sizes.html">Sizes</a>
              <a class="dropdown-item" href="faqs.html">FAQs</a>
              <a class="dropdown-item" href="contact-us.html">Contact Us</a>
              <a class="dropdown-item" href="about-us.html">About Us</a>
            </div>
          </div>
      </li>
               
                <!-- search box -->
              <form class="form-inline">
              <input class="form-control rounded" type="search" placeholder="What are you looking for?" aria-label="Search" size=60px style="text-align: left;">
              <button type="submit" class="btn btn-outline-dark" style="margin-left: 5px"><i class="bi bi-search"></i></button>
              </form>
                
                <!-- shopping cart -->
              <li class="nav-item">
                <a class="nav-link active" aria-current="page" href="cart.html">
                     <i class="bi bi-bag" style="font-size: 30px; margin-left: 650px;"></i>
                    <span class="cart-basket d-flex align-items-center justify-content-center">
                  0
                </span>
                </a>
              </li>
                
            </ul>
      
          </div>
        </div>
      </nav>



      <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>

  </body>

在页面底部添加脚本文件

【讨论】:

非常感谢!!!!

以上是关于Bootstrap 响应式菜单切换器的问题的主要内容,如果未能解决你的问题,请参考以下文章

Bootstrap 4 使用切换按钮连接侧面菜单(响应式)

bootstrap 3 响应式巨型菜单

Bootstrap 导航栏响应式下拉菜单不响应触摸

Bootstrap 4 灵活的响应式导航栏菜单

学生专用网页响应式网页设计动漫主题 bootstrap响应式设计动漫源码分享 附福利网页下载链接

带有 jQ​​uery 和 Bootstrap 3 的可折叠响应式侧边栏菜单