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.
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>
Effect demonstration
## 2. How to use js to display a drop-down menu when the mouse slides over itThe 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('.nav').children; for (var i = 0; i < lis.length; i++) { lis[i].onmouseover = function () { this.children[i].style.display = 'block'; } lis[i].onmouseout = function () { this.children[i].style.display = 'none'; } } </script> </body> </html>
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!