Home > Web Front-end > JS Tutorial > How to create a drop-down menu in JavaScript

How to create a drop-down menu in JavaScript

藏色散人
Release: 2021-07-03 10:01:28
Original
10961 people have browsed it

How to make a drop-down menu with JavaScript: 1. Use the value attribute to get the options of the drop-down menu; 2. Determine the status of the div based on the options; 3. Use the style.display style to hide or display the div.

How to create a drop-down menu in JavaScript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

How to create a drop-down menu with JavaScript?

js drop-down menu production

1. Use js to hide and display divs through drop-down menus

Idea: Use the value attribute Get the options of the drop-down menu → determine the status of the div based on the options → use the style.display style to hide or show the div. An example demonstration is as follows:

Code

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>W3Cschool(w3cschool.cn)</title>
</head>
<body>
  <select id="test_select">
    <option value="1">显示</option>
    <option value="2">隐藏</option>
  </select>
  <div id="test">我是一个div么呀我是一个div</div>
  <script>
    window.onload = function () {
      var obj_select = document.getElementById("test_select");
      var obj_div = document.getElementById("test");
      obj_select.onchange = function () {
        obj_div.style.display = this.value == 1 ? "block" : "none";
      }
    }
  </script>
</body>
</html>
Copy after login

Effect demonstration

How to create a drop-down menu in JavaScript

## 2. How to use js to display a drop-down menu when the mouse slides over it

The general idea is as follows: first set the width and height of the menu box and add position: relative; then set the same width and height for the content inside; then add absolute positioning of width to the drop-down secondary menu inside.

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>W3Cschool(w3cschool.cn)</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    li {
      list-style-type: none;
    }
    a {
      text-decoration: none;
      font-size: 14px;
    }
    .nav {
      margin: 100px;
    }
    .nav>li {
      position: relative;
      float: left;
      width: 80px;
      height: 41px;
      text-align: center;
    }
    .nav li a {
      display: block;
      width: 100%;
      height: 100%;
      line-height: 41px;
      color: #333;
    }
    .nav>li>a:hover {
      background-color: #eee;
    }
    .nav ul {
      display: none;
      position: absolute;
      top: 41px;
      left: 0;
      width: 100%;
      border-left: 1px solid #FECC5B;
      border-right: 1px solid #FECC5B;
    }
    .nav ul li {
      border-bottom: 1px solid #FECC5B;
    }
    .nav ul li a:hover {
      background-color: #FFF5DA;
    }
  </style>
</head>
<body>
  <ul>
    <li>
      <a href="javascript:;">下拉</a>
      <ul>
        <li><a href="javascript:;">下拉1</a></li>
        <li><a href="javascript:;">下拉2</a></li>
        <li><a href="javascript:;">下拉3</a></li>
        <li><a href="javascript:;">下拉4</a></li>
      </ul>
    </li>
  </ul>
  <script>
    var lis = document.querySelector(&#39;.nav&#39;).children;
    for (var i = 0; i < lis.length; i++) {
      lis[i].onmouseover = function () {
        this.children[i].style.display = &#39;block&#39;;
      }
      lis[i].onmouseout = function () {
        this.children[i].style.display = &#39;none&#39;;
      }
    }
  </script>
</body>
</html>
Copy after login
Recommended study: "

javascript Advanced Tutorial"

The above is the detailed content of How to create a drop-down menu in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template