目录

操作方法 - 搜索/过滤下拉菜单


了解如何使用 CSS 和 JavaScript 在下拉菜单中搜索项目。


过滤器下拉菜单

亲自试一试 »

创建可点击的下拉菜单

创建一个当用户单击按钮时出现的下拉菜单。

步骤1)添加HTML:

示例

<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
    <a href="#about">About</a>
    <a href="#base">Base</a>
    <a href="#blog">Blog</a>
    <a href="#contact">Contact</a>
    <a href="#custom">Custom</a>
    <a href="#support">Support</a>
    <a href="#tools">Tools</a>
  </div>
</div>

示例解释

使用任何元素打开下拉菜单,例如 <button>、<a> 或 <p> 元素。

使用容器元素(如 <div>)创建下拉菜单并在其中添加下拉链接。

将 <div> 元素包裹在按钮和 <div> 周围,以使用 CSS 正确定位下拉菜单。


步骤2)添加CSS:

示例

/* Dropdown Button */
.dropbtn {
  background-color: #04AA6D;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
  background-color: #3e8e41;
}

/* The search field */
#myInput {
  box-sizing: border-box;
  background-image: url('searchicon.png');
  background-position: 14px 12px;
  background-repeat: no-repeat;
  font-size: 16px;
  padding: 14px 20px 12px 45px;
  border: none;
  border-bottom: 1px solid #ddd;
}

/* The search field when it gets focus/clicked on */
#myInput:focus {outline: 3px solid #ddd;}

/* The container <div> - needed to position the dropdown content */
.dropdown {
  position: relative;
  display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f6f6f6;
  min-width: 230px;
  border: 1px solid #ddd;
  z-index: 1;
}

/* Links inside the dropdown */
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}

示例解释

我们使用背景颜色、填充、悬停效果等设计了下拉按钮的样式。

这个.dropdown类用途position:relative,当我们希望将下拉内容放置在下拉按钮的正下方时需要使用它(使用position:absolute)。

这个.dropdown-content类保存实际的下拉菜单。它默认隐藏,并将在悬停时显示(见下文)。请注意min-width设置为 230px。请随意更改此设置。提示:如果希望下拉内容的宽度与下拉按钮一样宽,请设置width至 100%(并且overflow:auto以在小屏幕上启用滚动)。

搜索字段 (#myInput) 的样式适合下拉菜单。我们添加了一个搜索图标,该图标位于搜索字段的左侧,表明这实际上是一个搜索字段。



步骤 3) 添加 JavaScript:

示例

/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

function filterFunction() {
  var input, filter, ul, li, a, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  div = document.getElementById("myDropdown");
  a = div.getElementsByTagName("a");
  for (i = 0; i < a.length; i++) {
    txtValue = a[i].textContent || a[i].innerText;
    if (txtValue.toUpperCase().indexOf(filter) > -1) {
      a[i].style.display = "";
    } else {
      a[i].style.display = "none";
    }
  }
}
亲自试一试 »

提示:去我们的CSS 下拉菜单教程了解有关下拉菜单的更多信息。

提示:去我们的悬停下拉菜单了解有关可悬停下拉菜单的更多信息