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

IT技术 javascript css reactjs navbar bootstrap-4
2021-05-15 08:36:10

为什么我的引导程序导航栏不折叠?切换按钮出现了,但是当我点击它时,什么也没有发生。(用 React 编写,所以使用 className 而不是 class。)我什至尝试直接从 Bootstrap 示例中复制和粘贴,但它仍然不起作用。我忘记在某处导入某些东西了吗?我还尝试从有关 stackoverflow 的另一个问题中复制和粘贴此示例,但没有奏效。

<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个回答

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

<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>

如果您对 jQuery 和 Bootstrap 脚本使用捆绑包,请确保以正确的顺序捆绑脚本。

也尝试包含 bootstrap.min.js

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

你需要加载 bootstrap js

bootstrap 中的某些功能取决于它,例如导航栏折叠

这里检查第四点

你的代码应该是这样的

<!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>