Bootstrap导航栏折叠切换按钮未单击

时间:2017-10-22 05:49:01

标签: javascript css reactjs navbar bootstrap-4

为什么我的引导导航栏没有崩溃?切换按钮显示但是当我点击它时,没有任何反应。 (用React编写,所以使用className而不是class。)我甚至尝试直接从Bootstrap示例复制和粘贴它仍然无法正常工作。我忘了在某处导入某些东西吗?我也试过在stackoverflow上从this other question复制和粘贴这个例子,但是没有用。

<nav className="navbar navbar-default navbar-fixed-top navbar-shrink">
    <div className="container">
        <div className="navbar-header page-scroll">
            <button type="button" className="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar=in-collapse" aria-expanded="false" aria-controls="navbar">
                <span className="icon-bar"></span>
                <span className="icon-bar"></span>
                <span className="icon-bar"></span>
            </button>
            <a className="navbar-logo page-scroll" href="#about">
                <img src="icon.png" />Page
            </a>
        </div>

        <div className="collapse navbar-collapse" id="navbar-in-collapse">
            <ul className="nav navbar-nav navbar-right">
                <li className="hidden active">
                    <a href="#page-top"></a>
                </li>
                <li className="">
                    <a className="page-scroll" href="#portfolio">Portfolio</a>
                </li>
                <li className="">
                    <a className="page-scroll" href="#blog">Blog</a>
                </li>
                <li className="">
                    <a className="page-scroll" href="#contact">Contact</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

在我的HTML中,它看起来像这样:

<!DOCTYPE HTML>

<html>

<head>
  <title>Kaisin Li</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link href="./index.css" rel="stylesheet" />
  <link rel="icon" href="./favicon.ico">
</head>

<body>
  <div id="main"></div>
  <script src="./bundle.js" defer></script>
</body>

</html>

谢谢!

3 个答案:

答案 0 :(得分:1)

尝试包括bootstrap.min.js

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0alpha.6/js/bootstrap.min.js" ></script>

答案 1 :(得分:1)

您需要加载bootstrap js

bootstrap中的某些功能取决于它,例如navbar collapse

检查第四点here

你的代码看起来应该是这样的

<!DOCTYPE HTML>

<html>

<head>
  <title>Kaisin Li</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link href="./index.css" rel="stylesheet" />
  <link rel="icon" href="./favicon.ico">
</head>

<body>
  <div id="main"></div>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  <script src="./bundle.js" defer></script>
</body>

</html>

答案 2 :(得分:1)

首先,您需要将 jQuery bootrstrap.js 的链接附加到您的页面。第二,data-target="#navbar=in-collapse"应替换为data-target="#navbar-in-collapse"。请参阅并运行下面的代码段。

&#13;
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<nav class="navbar navbar-default navbar-fixed-top navbar-shrink">
    <div class="container">
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-in-collapse" aria-expanded="false" aria-controls="navbar">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-logo page-scroll" href="#about">
                <img src="icon.png" />Page
            </a>
        </div>

        <div class="collapse navbar-collapse" id="navbar-in-collapse">
            <ul class="nav navbar-nav navbar-right">
                <li class="hidden active">
                    <a href="#page-top"></a>
                </li>
                <li class="">
                    <a class="page-scroll" href="#portfolio">Portfolio</a>
                </li>
                <li className="">
                    <a class="page-scroll" href="#blog">Blog</a>
                </li>
                <li class="">
                    <a class="page-scroll" href="#contact">Contact</a>
                </li>
            </ul>
        </div>
    </div>
</nav>
&#13;
&#13;
&#13;

如果你使用bundle for jQuery和Bootstrap scrips,那么shure脚本将以正确的顺序捆绑。