This article example shares the specific code of JavaScript to implement the drop-down list for your reference. The specific content is as follows This time I wrote a relatively simple implementation of a drop-down list. Click to display the list content, and click again to disappear the list. After studying it for a long time, I found that this js writing method is indeed easier to use. Let’s take a look at the effect first. Let’s go directly to the code. JS is the main part, and CSS can be debugged at will, but this writing method requires CSS. 1. HTML code <body> <!--Outermost layer--> <div class="outer"> <!-- Inside --> <div class="inner"> <h2>First</h2> <ul> <li>a</li> <li>b</li> <li>c</li> </ul> </div> <div class="inner"> <h2>Second</h2> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </div> <div class="inner"> <h2>Second</h2> <ul> <li>4</li> <li>5</li> <li>6</li> </ul> </div> <div class="inner"> <h2>Second</h2> <ul> <li>7</li> <li>8</li> <li>9</li> </ul> </div> <!-- Inside --> </div> <!--Outermost layer--> </body> 2. CSS code .outer{ margin: 0 auto; width: 500px; height: 600px; border: 1px solid red; } .outer .inner{ width: 500px; border: 1px solid red; } .outer .inner ul{ list-style: none; border: 1px solid fuchsia; } h2{ border: 1px solid blueviolet; height: 30px; display: flex; justify-content: center; cursor: pointer; background-color: #74a400; margin: 0; } ul{ display: none; } Here, .ul is not in HTML, so you need to add it through js. display: block; background-color: cornflowerblue; margin: 0; } ul li{ border: 1px solid cornflowerblue; background-color: darkgray; display: flex; justify-content: center; margin-left: -42px; cursor: pointer; } 3. The most important js code part window.onload = function () { // Get h2 and ul var h2 = document.getElementsByTagName("h2"); var ul = document.getElementsByTagName("ul"); //Bind a click event to all h2 for (let i = 0; i <h2.length; i++) { h2[i].index = i; h2[i].onclick = function () { //The bound event is that if the ul at the same level as h2 does not have a classname, its classname will be named ul. If it does, its classname will be set to empty. //Through the css code, you can see that there is a .ul part that does not work, because js has not yet changed the corresponding h2 classname. It will only change when h2 is clicked. //This writing method does not directly change the CSS style content, but realizes the style change by changing the name. In this way, one style can be used many times without having to write the style repeatedly. if (ul[this.index].className == ""){ ul[this.index].className = "ul"; }else { ul[this.index].className = ""; } } } } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Install Apple Mac OS X in VMWare12 Graphic Tutorial
>>: Enable remote access rights for MySQL under Linux and open port 3306 in the firewall
I just tried it on IE6, and it does show the toolb...
The multi-site feature of WordPress allows you to...
MySQL is divided into installation version and fr...
1. What is Vue Vue is a progressive framework for...
There are many tags and elements in the HTML head ...
Currently, almost all large websites and applicat...
There is a business that queries the 5 most recen...
Preface The mv command is the abbreviation of mov...
History always repeats itself surprisingly well. ...
On a whim, I wrote a case study of a small ball b...
Neo4j (one of the Nosql) is a high-performance gr...
Create a simple Spring boot web project Use the i...
500 (Internal Server Error) The server encountere...
Preface If the query information comes from multi...
Table of contents Preface call usage accomplish A...