我的下拉菜单无法使用引导程序工作

时间:2020-01-22 06:35:22

标签: javascript html css twitter-bootstrap bootstrap-4

我创建了一个下拉菜单,该下拉菜单将由搜索栏上的按钮触发,并且在搜索栏下方将部署下拉菜单内容。虽然我的代码行不通

<div class="col-8">
  <div class="input-group mb-3">
    <div class="input-group-prepend" id="button-addon3">
      <div class="dropdown">
        <button class="btn btn-outline-secondary " type="button" style="color:black;background-color: darkcyan">Search</button>
        <div id="myDropdown" class="dropdown-content">
          <a href="#home">Home</a>
          <a href="#about">About</a>
          <a href="#contact">Contact</a>
        </div>
      </div>
      <button class="btn btn-outline-secondary" type="button" style="color:black;background-color: darkcyan" onCLick="myFunction()">Filter</button>
    </div>
    <input type="text" class="form-control" placeholder="Search Reports Here" aria-label="Example text with two button addons" aria-describedby="button-addon3">
  </div>
</div>
function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
    }
    window.onclick = function(event) {
        if (!event.target.matches('.dropbtn')) {
            var dropdowns = document.getElementsByClassName("dropdown-content");
            var i;
            for (i = 0; i < dropdowns.length; i++) {
            var openDropdown = dropdowns[i];
            if (openDropdown.classList.contains('show')) {
                openDropdown.classList.remove('show');
            }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您好,如果您正在使用引导程序,则可以使用引导程序下拉菜单本身,请使用以下代码,而无需编写脚本。 :)

<div class="input-group mb-3">
  <div class="input-group-prepend">
    <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</button>
    <div class="dropdown-menu">
      <a class="dropdown-item" href="#">Action</a>
      <a class="dropdown-item" href="#">Another action</a>
      <a class="dropdown-item" href="#">Something else here</a>
      <div role="separator" class="dropdown-divider"></div>
      <a class="dropdown-item" href="#">Separated link</a>
    </div>
<button class="btn btn-outline-secondary" type="button">Filter</button>
  </div>
  <input type="text" class="form-control" aria-label="Text input with dropdown button">
</div>