search
HomeWeb Front-endHTML TutorialNative js implementation of drop-down list box

Native js implementation of drop-down list box

Nov 17, 2017 pm 02:53 PM
javascriptdrop downlist

Imitateqq listClick the drop-down,jsNative implementation,Free source codeprovides research, take it! Follow PHP中文网 to give you more interesting things!

Native js implementation of drop-down list box

Code:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHP中文网--下拉框</title>
<style>
ul , h2 { padding:0; margin:0; }
li { list-style:none; }
#list { width:240px; border:1px solid #333; margin:0 auto; }
#list .lis {}
#list h2 { height:30px; line-height:30px; text-indent:20px; background:yellow; color:#000; }
#list .active { background:orange; color:#000; }
#list ul { display:none; }
#list ul li { line-height:24px; border-bottom:1px solid #333; text-indent:24px; }
#list ul .hover { background:pink; }
</style>
<script>
window.onload = function (){
	var oUl = document.getElementById(&#39;list&#39;);
	var aH2 = oUl.getElementsByTagName(&#39;h2&#39;);
	var aUl = oUl.getElementsByTagName(&#39;ul&#39;);
	var aLi = null;
	var arrLi = [];
	for(var i=0;i<aH2.length;i++){
		aH2[i].index=i;
		aH2[i].onclick = function(){
			for(var i=0;i<aH2.length;i++){
				if(aH2[i] !=this){
					aUl[i].style.display=&#39;none&#39;;
					aH2[i].className=&#39;&#39;;
				}
			}
			if(this.className==&#39;&#39;){
				aUl[this.index].style.display=&#39;block&#39;;
				this.className=&#39;active&#39;;
			}else{
				aUl[this.index].style.display=&#39;none&#39;;
				this.className=&#39;&#39;;
			}
		};
	}
	for(var i=0;i<aUl.length;i++){
		aLi = aUl[i].getElementsByTagName(&#39;li&#39;);
		for(var j=0;j<aLi.length;j++){
			arrLi.push(aLi[j]);
		}
	}
	for(var i=0;i<arrLi.length;i++){
		arrLi[i].onclick=function(){
			for(var i=0;i<arrLi.length;i++){
				if(arrLi[i] !=this){
					arrLi[i].className=&#39;&#39;;
				}
			}
			if(this.className==&#39;&#39;){
				this.className=&#39;hover&#39;;
			}else{
				this.className=&#39;&#39;;
			}
		};
	}
};
</script>
</head>

<body>

<ul id="list" style="margin-top:50px;">
	<li class="lis">
  	<h2 id="我的好友">我的好友</h2>
    <ul>
    	<li>a111</li>
    	<li>a222</li>
    	<li>a333</li>
    	<li>a444</li>
    </ul>
  </li>
	<li class="lis">
  	<h2 id="陌生人">陌生人</h2>
    <ul>
    	<li>b111</li>
    	<li>b222</li>
    	<li>b333</li>
    	<li>b444</li>
    	<li>b555</li>
    </ul>
  </li>
	<li class="lis">
  	<h2 id="黑名单">黑名单</h2>
    <ul>
    	<li>c111</li>
    	<li>c222</li>
    </ul>
  </li>
</ul>

</body>
</html>

Get it for free and study it! More good source codes are available on PHP Chinese website, follow us and give you a good look~

Related recommendations:

css, js dice lottery source code

html moving puppy source code

js original sound Implementing a simple WeChat chat function


The above is the detailed content of Native js implementation of drop-down list box. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.