Bootstrap 导航栏在尝试使其可折叠时出现故障

Bootstrap navbar glitching while trying to make it collapsible

我从 w3schools.com 那里得到了 导航栏 的示例代码。它正在使用 bootstrap。这是代码:

<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>                        
      </button>
      <a class="navbar-brand" href="#">WebSiteName</a>
    </div>
    
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Page 1</a></li>
        <li><a href="#">Page 2</a></li>
        <li><a href="#">Page 3</a></li>
      </ul>
    </div>

  </div>
</nav>

这个导航栏工作得很好。 但是对于小屏幕(可折叠 导航栏的情况)我希望导航栏在我单击其中的一个选项时折叠。因此,我在所有 <li> 中添加了 data-toggle="collapse" data-target="#myNavbar"。现在导航栏可以针对小屏幕正确切换。

这是问题的视频:

修改后的代码如下:

<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>                        
      </button>
      <a class="navbar-brand" href="#">WebSiteName</a>
    </div>
    
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li class="active"><a data-toggle="collapse" data-target="#myNavbar"  href="#">Page 1</a></li>
        <li><a data-toggle="collapse" data-target="#myNavbar" href="#">Page 2</a></li>
        <li><a data-toggle="collapse" data-target="#myNavbar" href="#">Page 3</a></li>
      </ul>
    </div>

  </div>
</nav>

但是现在,整个导航栏在大屏幕(桌面)上都出现故障。请帮助解决这个问题。

请指出这是不是复制,因为我找不到。

桌面屏幕出现故障的最终 HTML 代码在下方(google-chrome、firefox、brave 中存在相同问题):

<html>
  <title>Example</title>    
<body>
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    </head>
    <body>
    
    <nav class="navbar navbar-inverse">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>                        
          </button>
          <a class="navbar-brand" href="#">WebSiteName</a>
        </div>
        
        <div class="collapse navbar-collapse" id="myNavbar">
          <ul class="nav navbar-nav">
            <li class="active"><a data-toggle="collapse" data-target="#myNavbar"  href="#">Home</a></li>
            <li><a data-toggle="collapse" data-target="#myNavbar" href="#">Page 2</a></li>
            <li><a data-toggle="collapse" data-target="#myNavbar"  href="#">Page 3</a></li>
          </ul>
        </div>

      </div>
    </nav>
      
    <div class="container">
      <h3>Collapsible Navbar</h3>
      <p>In this example, the navigation bar is hidden on small screens and replaced by a button in the top right corner (try to re-size this window).
      <p>Only when the button is clicked, the navigation bar will be displayed.</p>
    </div>
    
    </body>
    </html>
    </body>
</html>

单击 展开代码段 以查看桌面屏幕上的故障。

有一种更好的方法来折叠导航栏!为什么不包括一些 JS 行,因为你已经在使用 jQuery,就像这样-

$('.navbar-nav>li>a').on('click', function() {
  $('.navbar-collapse').collapse('hide');
});
<title>Example</title>

<body>
  <!DOCTYPE html>
  <html lang="en">

  <head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
  </head>

  <body>

    <nav class="navbar navbar-inverse">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>                        
      </button>
          <a class="navbar-brand" href="#">WebSiteName</a>
        </div>

        <div class="collapse navbar-collapse" id="myNavbar">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Page 1</a></li>
            <li><a href="#">Page 2</a></li>
            <li><a href="#">Page 3</a></li>
          </ul>
        </div>

      </div>
    </nav>


    <div class="container">
      <h3>Collapsible Navbar</h3>
      <p>In this example, the navigation bar is hidden on small screens and replaced by a button in the top right corner (try to re-size this window).
        <p>Only when the button is clicked, the navigation bar will be displayed.</p>
    </div>

  </body>

  </html>
</body>