You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bootstrap 4导航栏切换按钮缩小时失效且不显示列表

Fixing Bootstrap 4 Navbar Toggler Not Working on Window Shrink

Hey Franky, let's troubleshoot why your navbar toggler isn't popping up the menu when the browser window gets smaller. This is a super common issue with Bootstrap 4, and it almost always comes down to one of these easy-to-fix problems:

1. Double-Check Your Script Loading Order (and jQuery Version)

Bootstrap's JavaScript relies on jQuery to work properly. While your code does load jQuery before Bootstrap JS (good call!), make sure you're using a jQuery version compatible with Bootstrap 4—that's jQuery 3.1.1 or newer. Older versions can break the toggler functionality.

If you're unsure, swap out your current jQuery script for a supported version (just make sure it's loaded before Bootstrap JS).

2. Verify Your Navbar HTML Structure

Most of the time, the issue is incorrect markup. Bootstrap 4 requires a specific structure for the toggler and collapsible menu to hook up correctly. Here's the proper template to compare against your code:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <!-- Brand/logo -->
  <a class="navbar-brand" href="#">Your Brand</a>

  <!-- Toggler button - must have these attributes -->
  <button class="navbar-toggler" type="button" 
          data-toggle="collapse" 
          data-target="#navbarMenu" 
          aria-controls="navbarMenu" 
          aria-expanded="false" 
          aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <!-- Collapsible menu container - ID must match data-target above -->
  <div class="collapse navbar-collapse" id="navbarMenu">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">About</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Contact</a>
      </li>
    </ul>
  </div>
</nav>

Key things to check in your code:

  • The toggler button has data-toggle="collapse" and data-target="#yourMenuId"
  • The collapsible menu container has an id that exactly matches the data-target value (case-sensitive!)
  • All your nav links are inside the .collapse.navbar-collapse div

3. Check for File Loading Errors

Open your browser's dev tools (F12) and go to the Network tab. Refresh the page and make sure:

  • bootstrap.min.css and bootstrap.js are loading without 404 errors (double-check your file paths!)
  • jQuery is loading successfully

If any of these files are missing, the toggler won't work because the required styles or scripts aren't there.

4. Rule Out Conflicting Code

Sometimes custom CSS or other JavaScript can interfere with Bootstrap's functionality. Try temporarily removing any custom <style> blocks or extra scripts you have. If the toggler starts working, you'll know the issue is a conflict—then you can slowly add your code back in to find the culprit.

Give these steps a try, and your navbar toggler should start working as expected!

内容的提问来源于stack exchange,提问作者Franky Padilla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:31:05