gpt4 book ai didi

javascript - 为什么我的导航栏折叠切换在 react 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:31 26 4
gpt4 key购买 nike

我想创建一个响应式导航栏。但是切换不起作用。我该怎么办?

我添加了 bootstrap 文档中指定的 bootstrap、jQuery 和 popper.js 链接,并创建了一个导航栏组件。导航栏的样式与未折叠时应有的样式相同,但折叠后切换不起作用。

import React, { Component } from 'react';

import "jquery/dist/jquery"
import "bootstrap/dist/js/bootstrap";
import 'bootstrap/dist/css/bootstrap';

class Navbar extends Component {
render() {
return (
<React.Fragment>

<nav className="navbar navbar-expand-lg navbar-light bg-light">
<a className="navbar-brand" href="/">Aditya Chopra</a>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="/navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>

<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav mr-auto">

<li className="nav-item">
<a className="nav-link" href="/About">About</a>
</li>
<li className="nav-item">
<a className="nav-link" href="/Contact">Contact</a>
</li>
<li className="nav-item active">
<a className="nav-link" href="/Projects">Projects </a>
</li>
</ul>
</div>
</nav>
</React.Fragment>
);
}
}

export default Navbar;

index.html:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />

<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">
<title>Aditya Chopra</title>
</head>

<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

当视口(viewport)足够小时,我希望它通过切换按钮扩展导航栏,但它什么也没做。

最佳答案

您的data-target 属性在这里设置不正确,

<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="/navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>

上面代码中的问题是 data-target="/navbarSupportedContent" 属性。

当您尝试使用 id 将您的 toggler icon 与您的菜单选项连接时,您应该使用 # 作为 iddata-target="#navbarSupportedContent"相反,您使用的是 /,这就是您的 toggler icon 无法正常工作的原因。

关于javascript - 为什么我的导航栏折叠切换在 react 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56207103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com