为什么我的引导导航栏没有折叠?切换按钮显示,但当我单击它时,没有任何反应。 (用 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>
谢谢!
首先,您需要将 jQuery 和 bootrstrap.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
引导程序中的一些功能依赖于它,例如导航栏折叠
检查第四点这里
你的代码应该是这样的
<!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>
在您的代码中,data-target="#navbar=in-collapse" 有“=”,id 是 id="navbar-in-collapse",没有“=”,它是“-”。检查拼写错误。