为什么切换按钮在引导导航栏中不起作用?

时间:2021-07-15 04:17:12

标签: html bootstrap-4

我正在使用切换按钮进行响应式设计。我不明白为什么点击切换按钮不起作用。

<!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.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
</head>

<body>
    <nav class="navbar navbar-expand-md navbar-light bg-light ">
        <a class="navbar-brand " href=" ">hello</a>
        <button class="navbar-toggler " type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01 " aria-controls="navbarTogglerDemo01 " aria-expanded="false ">
            <span class="navbar-toggler-icon "></span>
          </button>
        <div class="collapse navbar-collapse" id="navbarTogglerDemo01">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item ">
                    <a class="nav-link " href=" ">home
                </a>
                </li>
                <li class="nav-item ">
                    <a class="nav-link " href=" ">
                    contact
                </a>
                </li>
            </ul>
        </div>
    </nav>
</body>

</html>

2 个答案:

答案 0 :(得分:0)

在关闭 </body> 标记之前,在您的 html 中添加 jQuery 和 Bootstrap javascript。像这样:这里还有工作演示 https://jsfiddle.net/hw1Ldvt7/

<!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.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
</head>

<body>
    <nav class="navbar navbar-expand-md navbar-light bg-light ">
        <a class="navbar-brand " href=" ">hello</a>
        <button class="navbar-toggler " type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01 " aria-controls="navbarTogglerDemo01 " aria-expanded="false ">
            <span class="navbar-toggler-icon "></span>
          </button>
        <div class="collapse navbar-collapse" id="navbarTogglerDemo01">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item ">
                    <a class="nav-link " href=" ">home
                </a>
                </li>
                <li class="nav-item ">
                    <a class="nav-link " href=" ">
                    contact
                </a>
                </li>
            </ul>
        </div>
    </nav>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js" integrity="sha512-XKa9Hemdy1Ui3KSGgJdgMyYlUg1gM+QhL6cnlyTe2qzMCYm4nAZ1PsVerQzTTXzonUR+dmswHqgJPuwCq1MaAg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</body>

</html>

答案 1 :(得分:0)

需要在bootstrap中引入jQuery Bootstrap = 样式库 + 一堆 jQuery 插件

顺便说一句,如果你只需要bootstrap样式,完全不用为jQuery和bootstrap引入JS文件