Summary:
The jquery library has brought us a lot of conveniences. It is very simple to use jquery to implement a simple drop-down menu, but there are also different implementation methods. Today I used jquery to write a drop-down menu. I referred to Xiaofeng Wang's SexyDropDownMenu2010. There are still some things in it that I feel are worth recording.
Implementation:
First, post his code (it’s too long to post the whole code, so just pick up some parts),
1. ul list in html
<ul class="topmenu"> <li><a href="#">Home</a></li> <li><a href="#">Tutorials</a> <ul class="submenu1"> <li><a href="#">Ch1</a></li> <li><a href="#">Ch2</a> <ul class="submenu11"> <li><a href="#">Ch21</a> <ul class="submenu11"> <li><a href="#">Ch211</a> <ul class="submenu11"> <li><a href="#">Ch2111</a> <ul class="submenu11"> <li><a href="#">Ch21111</a></li> <li><a href="#">Ch21112</a></li> <li><a href="#">Ch21113</a></li> <li><a href="#">Ch21114</a></li> <li><a href="#">Ch21115</a></li> <li><a href="#">Ch21116</a></li> </ul> </li> <li><a href="#">Ch2112</a></li> <li><a href="#">Ch2113</a></li> <li><a href="#">Ch2114</a></li> <li><a href="#">Ch2115</a></li> </ul> </li> <li><a href="#">Ch212</a></li> <li><a href="#">Ch213</a></li> <li><a href="#">Ch214</a></li> </ul> </li> <li><a href="#">Ch22</a> <ul class="submenu11"> <li><a href="#">Ch221</a></li> <li><a href="#">Ch222</a></li> <li><a href="#">Ch223</a></li> </ul> </li> <li><a href="#">Ch23</a></li> </ul> </li> <li><a href="#">Ch3</a> <ul class="submenu11"> <li><a href="#">Ch31</a></li> <li><a href="#">Ch32</a></li> </ul> </li> </ul> </li> <li><a href="#">Resources</a> <ul class="submenu1"> <li><a href="#">Sub Nav Link</a></li> <li><a href="#">Sub Nav Link</a></li> </ul> </li> <li><a href="#">About Us</a></li> <li><a href="#">Advertise</a></li> <li><a href="#">Submit</a></li> <li><a href="#">Contact Us</a></li> </ul>
It is a six-level menu structure, as shown below
2. js part (css will not be posted)
$(document).ready(function() { //第一部分 // Top Menu //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.submenu1*) $("ul.submenu1").parent().append("<span></span>"); //第二部分 $("ul.topmenu li span").click(function() { //When trigger is clicked... //Following events are applied to the submenu1 itself (moving submenu1 up and down) //Drop down the submenu1 on click $(this).parent().find("ul.submenu1").slideDown('fast').show(); //在click后给绑定hover处理函数,感觉是比较巧妙的地方 $(this).parent().hover(function() { }, function() { //When the mouse hovers out of the submenu1, move it back up $(this).parent().find("ul.submenu1").slideUp('slow'); }); //Following events are applied to the trigger (Hover events for the trigger) }).hover(function() { //On hover over, add class "hover" $(this).addClass("hover"); }, function() { //On Hover Out //On hover out, remove class "hover" $(this).removeClass("hover"); }); //第三部分 $("ul.topmenu li ul.submenu1 li").hover(function() { $(this).find("ul.submenu11:first").show("slow"); }, function() { $(this).find("ul.submenu11:first").hide("fast"); }); });
Part One:
Added a trigger button under the menu
Part 2:
Bound a handler function for click event
After the trigger button is clicked, the hover processing function is bound to the outermost li of the drop-down menu, which feels quite clever.
Bind the hover function to the outermost li. The processing function here is written so that no processing is performed when the mouse hovers over the li (the first function is empty), and the li is closed when the mouse leaves.
In this way, the menu that expands ul later will be inside the outermost li, so that the menu will not be automatically closed, which means that when the mouse leaves the entire menu, the li will be automatically closed.
Part Three:
Bind a function to the hover event of the nested ul under the menu to expand and collapse the next level menu
$(this).find("ul.submenu11:first") is also more clever to obtain the descendants of each element in the current matching element set through find, and filter the next generation elements through "ul.submenu11:first" Expand.
At the same time, a collapsing processing function is also bound to the next-level menu, which works together with the collapsing function bound to the outermost li by click in the trigger menu. It is the automatic collapsing function of the menu.
Summary:
It shows the power and flexibility of jquery filter, and also reflects the beautiful chain syntax of jquery.
Let me share with you a pure jQuery horizontal drop-down menu implementation
<!DOCTYPE html> <html> <head> <title>jQuery水平下拉菜单实现</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" charset="UTF-8" > <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="bootstrap/js/jquery-1.11.1.min.js"></script> <!--[if lt IE 9]> <script src="bootstrap/js/html5shiv.js"></script> <script src="bootstrap/js/respond.min.js"></script> <![endif]--> <style type="text/css"> .menus{border:1px solid red; float:left; margin-left:4px; background:red;} .menus a{display:block; width:100px; text-align:center;} .menu{display:none;} a{cursor:pointer;text-decoration:none;} a:hover{background:white; text-decoration:none;} a:visited{text-decoration:none; color:black;} </style> <script> $(function(){ $(".menu-title").click(function(){ $(this).next().toggleClass(); }); }); </script> </head> <body> <div class="menus"> <a class="menu-title">菜单项</a> <div class="menu"> <a href="#" class="menu-item">菜单列表</a> <a href="#" class="menu-item">菜单列表</a> <a href="#" class="menu-item">菜单列表</a> <a href="#" class="menu-item">菜单列表</a> </div> </div> <div class="menus"> <a class="menu-title">菜单项</a> <div class="menu"> <a href="#" class="menu-item">菜单列表</a> <a href="#" class="menu-item">菜单列表</a> <a href="#" class="menu-item">菜单列表</a> <a href="#" class="menu-item">菜单列表</a> </div> </div> <div class="menus"> <a class="menu-title">菜单项</a> <div class="menu"> <a href="#" class="menu-item">菜单列表</a> <a href="#" class="menu-item">菜单列表</a> <a href="#" class="menu-item">菜单列表</a> <a href="#" class="menu-item">菜单列表</a> </div> </div> <div class="menus"> <a class="menu-title">菜单项</a> <div class="menu"> <a href="#" class="menu-item">菜单列表</a> <a href="#" class="menu-item">菜单列表</a> <a href="#" class="menu-item">菜单列表</a> <a href="#" class="menu-item">菜单列表</a> </div> </div> </body> </html>

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.