Bootstrap 4导航栏切换不起作用

时间:2019-03-22 04:35:30

标签: html bootstrap-4

这是我的Bootstrap文档中用于响应式导航栏的代码。但是,当我将浏览器窗口(在Chrome / Safari中)设置为移动设备大小时,切换下拉菜单似乎不起作用。

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Title</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarText">
    <ul class="navbar-nav mr-auto">
    <li class="nav-item active">
    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
    <a class="nav-link" href="#">Features</a>
    </li>
    <li class="nav-item">
    <a class="nav-link" href="#">Pricing</a>
    </li>
    </ul>
    <span class="navbar-text">
    Navbar text with an inline element
    </span>
    </div>
    </nav>

2 个答案:

答案 0 :(得分:0)

添加CSS和JS文件可以正常工作

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" ></script>


<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Title</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarText">
    <ul class="navbar-nav mr-auto">
    <li class="nav-item active">
    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
    <a class="nav-link" href="#">Features</a>
    </li>
    <li class="nav-item">
    <a class="nav-link" href="#">Pricing</a>
    </li>
    </ul>
    <span class="navbar-text">
    Navbar text with an inline element
    </span>
    </div>
    </nav>

https://jsfiddle.net/y5n0xot1/

答案 1 :(得分:0)

不建议在Angular Project中添加jquery,因为angular操作DOM渲染页面的方式与jquery的方式不同。

因此,这是解决此问题的最佳方法:

第1步::在添加引导程序4之后,在您的角度项目中安装ngx-bootstrap

  

npm install ngx-bootstrap --save

步骤2::在app.module.ts中:从'ngx-bootstrap / collapse'导入{CollapseModule};

将CollapseModule作为根注入,以便其在所有组件和模块中都可用。

  

进口:[       ...,       CollapseModule.forRoot()]

第3步::在div上添加折叠指令,以点击单击 [额外:您可以通过isAnimated输入选项启用动画]

 <button (click)="isNavbarCollapsed = !isNavbarCollapsed" class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" [attr.aria-expanded]="!isNavbarCollapsed" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div [collapse]="isNavbarCollapsed"  class="collapse navbar-collapse" id="navbarResponsive" [isAnimated]="true">
      <ul class="navbar-nav ml-auto">
        <li class="nav-item">
          <a class="nav-link" href="about.html">About</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="services.html">Services</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="contact.html">Contact</a>
        </li>
      </ul>
    </div>

步骤4:在components.ts中初始化“ isNavbarCollapsed”

isNavbarCollapsed = true;