我對 HTML 非常陌生,幾個小時以來我一直在努力讓我的下拉式選單正常運作。我搜索了許多面臨類似問題的帖子,但仍然找不到並解決問題。我不確定問題是否也是由我的腳本引起的
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> <header class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="#" id="logo">Logo</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"> <span class="navbar-toggler-icon"></span> </button> <nav class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"> <li class="nav-item"><a class="nav-link active" href="#">Home <span class="sr-only">(current)</span></a></li> <li class="nav-item"><a class="nav-link" href="#">Link</a></li> <li class="nav-item"> <div class="dropdown"> <a href="#" class="nav-link dropdown-toggle" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a> <div class="dropdown-menu" aria-labelledby="navbarDropdown"> <a href="#" class="dropdown-item">Action</a> <a href="#" class="dropdown-item">Another action</a> <div class="dropdown-divider"></div> <a href="#" class="dropdown-item">Something else here</a> </div> </div> </li> <li class="nav-item"><a class="nav-link" href="#">Disabled</a></li> </ul> </nav> </header>
提前致謝!
我嘗試用data-bs-toggle="dropdown"
取代data-toggle="dropdown"
但它不起作用,因為我使用的是bootstrap 4,直接從bootstrap 複製並貼上程式碼,它仍然失敗
P粉0116843262024-04-03 12:57:46
CDN 不適用於您的 jQuery。 Bootstrap 需要它才能運作。您在控制台中得到了一條線索,應該有一個錯誤表明它需要 jQuery。所以你可以:
替換這個
https://ajax.googleleapis.com /ajax/libs/jquery/3.5.1/jquery.min.js
#有了這個
https://code.jquery.com/jquery-3.6.1 .min.js