单击按钮时引导下拉菜单未打开

Posted

技术标签:

【中文标题】单击按钮时引导下拉菜单未打开【英文标题】:Bootstrap dropdown not opening when clicking button 【发布时间】:2022-01-22 12:29:53 【问题描述】:

我正在尝试使用带有多个选项的 Bootstrap 创建一个下拉菜单,但是当我单击该按钮时,没有任何内容可以选择该选项。不知道发生了什么。

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>A Basic HTML5 Template</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet" >
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" ></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
</head>

<body>
  <div class="dropdown">
              <button class="btn btn-secondary dropdown-toggle" type="button" id="SelectOS" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Select option...</button>
                <div class="dropdown-menu" aria-labelledby="SelectOS">
                  <a class="dropdown-item" href="#">Linux</a>
                  <a class="dropdown-item" href="#">Mac</a>
                  <a class="dropdown-item" href="#">Windows</a>
                </div>
  </div>
</body>
</html>

【问题讨论】:

【参考方案1】:

使用data-bs-toggle 属性而不是data-toggle 属性。

<!doctype html>

<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>A Basic HTML5 Template</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
</head>

<body>
  <div class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" type="button" id="SelectOS" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Select option...</button>
    <div class="dropdown-menu" aria-labelledby="SelectOS">
      <a class="dropdown-item" href="#">Linux</a>
      <a class="dropdown-item" href="#">Mac</a>
      <a class="dropdown-item" href="#">Windows</a>
    </div>
  </div>
</body>

</html>

【讨论】:

以上是关于单击按钮时引导下拉菜单未打开的主要内容,如果未能解决你的问题,请参考以下文章

引导下拉菜单按钮在单击按钮以及单击屏幕时更改

在内部单击时保持 Bootstrap 下拉菜单打开

在输入焦点上打开引导下拉菜单

多级下拉引导

Jquery下拉菜单单击始终打开

导航栏中的引导下拉菜单未打开