本文介绍了Bootstrap导航栏折叠切换按钮未单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的引导导航栏没有崩溃?切换按钮出现了,但是当我单击它时,什么也没有发生. (在React中编写,所以使用className而不是class.)我什至尝试直接从Bootstrap示例进行复制和粘贴,但仍然无法正常工作.我忘了在某处导入东西吗?我还尝试从这个其他问题上的该示例复制和粘贴,没用.

Why isn't my bootstrap navbar not collapsing? The toggle button shows up but when I click it, nothing happens. (Written in React so using className instead of class.) I even tried copying and pasting directly from the Bootstrap example and it still doesn't work. Am I forgetting to import something somewhere? I also tried copying and pasting from this example from this other question on stackoverflow, didn't work.

<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中,它看起来像这样:

In my html, it looks like this:

<!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".请参阅并运行下面的代码段.

In first, you need to append links to jQuery and bootrstrap.js to your page. In second, data-target="#navbar=in-collapse" should be replaced by data-target="#navbar-in-collapse". See the and run the snippet below.

<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脚本,请确保按正确的顺序捆绑脚本.

If you use bundles for jQuery and Bootstrap scrips, make shure the scripts are bundled in correct order.

这篇关于Bootstrap导航栏折叠切换按钮未单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:44