javascript - Bootstrap 按钮切换不打开 LI

标签 javascript jquery html css twitter-bootstrap

我使用 Bootstrap 创建了这个页面。当我将页面拖动到更窄的尺寸时,导航栏消失并被一个按钮取代。这个按钮的想法是,一旦点击它就会打开导航选项。这最后一点没有发生,我似乎无法找到解决办法。我一直在浏览网页多年但没有成功。请帮忙。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->

    <title>Grid System 3</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">



    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>

      .box {
        border: 1px solid grey;
        background-color: #D3D3D3;
      }

    </style>

  </head>
  <body>

    <div class="navbar navbar-inverse">

      <div class="container">

        <div class="navbar-header">
          <a href="" class="navbar-brand">My Website</a>

          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">

          <span class="sr-only">Toggle navigation</span>

          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>

          </button>

        </div>

        <div class="collapse navbar-collapse">

          <ul class="nav navbar-nav">
            <li><a href="">Page 1</a></li>
            <li><a href="">Page 2</a></li>
            <li><a href="">Page 3</a></li>
          </ul>

        </div>

      </div>

    </div>

    <h1>Hello, world!</h1>

    <div class="container">

      <div class="row">
        <div class="col-md-4 box">.col-md-4</div>
        <div class="col-md-4 box">.col-md-4</div>
        <div class="col-md-4 box">.col-md-4</div>
      </div>


    </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>

  </body>
</html>

最佳答案

要查找的最明显的事情是您有一个名为 js 的文件夹,其中包含 bootstrap min js 并称为 bootstrap.min.js 并且因为您是从 CDN 调用 jQuery,所以您需要有一个事件的连接到网络,而不是仅在本地服务器上工作。

您还需要在导航栏折叠上放置一个 id 并将其作为按钮的目标,因为页面中有超过 1 个类为“navbar”的元素。

如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->

    <title>Grid System 3</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>

      .box {
        border: 1px solid grey;
        background-color: #D3D3D3;
      }

    </style>

  </head>
  <body>

    <div class="navbar navbar-inverse">

      <div class="container">

        <div class="navbar-header">
          <a href="" class="navbar-brand">My Website</a>

          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">

          <span class="sr-only">Toggle navigation</span>

          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>

          </button>

        </div>
        <div id="navbar" class="navbar-collapse collapse">  


          <ul class="nav navbar-nav">
            <li><a href="">Page 1</a></li>
            <li><a href="">Page 2</a></li>
            <li><a href="">Page 3</a></li>
          </ul>

        </div>

      </div>

    </div>

    <h1>Hello, world!</h1>

    <div class="container">

      <div class="row">
        <div class="col-md-4 box">.col-md-4</div>
        <div class="col-md-4 box">.col-md-4</div>
        <div class="col-md-4 box">.col-md-4</div>
      </div>


    </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>


  </body>
</html>

我已经对其进行了测试,它按预期工作。

关于javascript - Bootstrap 按钮切换不打开 LI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35505119/

相关文章:

javascript - Rest api 实例中的临时评估问题

javascript - 为什么我的插件不加载它的依赖项?

javascript - 在canplaythrough 上淡出 HTML5 视频海报图像

php - 更改表单选择时的样式表 onChange ="change()"

javascript - 进入特定的全屏部分时是否可以更改标题背景?

css - 在响应式 View 中对齐列表项

javascript - 在 asp 中使用 Javascript 变量 :textbox text parameter

javascript - Jquery UI 按钮问题

javascript - Youtube 品牌 channel 中的 HTML5 window.postMessage

javascript - 如何解析 querySelectorAll 选择器引擎的结果并允许方法链接?