


How does JavaScript achieve the effect of highlighting the current menu in the navigation bar when it is selected?
This article mainly introduces the JS implementation of the highlighted navigation bar effect after selecting the current menu. It involves JavaScript traversal and style dynamic operation techniques for page elements. It has certain reference value. Friends in need can refer to it
The example of this article describes the JS implementation of the highlighted navigation bar effect after the current menu is selected. Share it with everyone for your reference. The details are as follows:
Here is a demonstration that after the current menu is selected, the menu is highlighted. That is to say, without changing the menu CSS code, JavaScript is used to control the background of the menu. If the menu item is clicked, , will give it a distinctive background color or background image, which can clearly guide the user to the website column they are browsing. This menu does not require the addition of dynamic language, it is simple, convenient and effective.
The screenshot of the running effect is as follows:
The specific code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>选中当前菜单后高亮</title> <style type="text/css"> <!-- .nav { MARGIN: 1px 0; WIDTH: 100%; FONT-FAMILY: verdana; HEIGHT: 21px; BACKGROUND-COLOR: #970B0B; font-family:Arial, Helvetica, sans-serif; font-size:12px; } .nav UL { PADDING: 0px; DISPLAY: block; MARGIN: 0px; LIST-STYLE-TYPE: none; HEIGHT: 21px; BACKGROUND-COLOR: #970B0B; COLOR: #ffffff; } .nav LI { BORDER-RIGHT: #ffffff 1px solid; DISPLAY: block; FLOAT: left; HEIGHT: 21px; font-family:Arial, Helvetica, sans-serif; font-size:12px; } .nav LI A { PADDING:1px 15px 0; DISPLAY: block; FONT-WEIGHT: none; COLOR: #ffffff; LINE-HEIGHT: 20px; TEXT-DECORATION: none; } .nav LI A:hover { COLOR:#562505; BACKGROUND-COLOR: #f4f524; TEXT-DECORATION: none; } .current{ color:#ffffff; background:#D42524; } .nav li#date{ color:#ffffff; PADDING:2px 15px 0; } --> </style> <script language="javascript" type="text/javascript"> var $c=function(array){var nArray = [];for (var i=0;i<array.length;i++) nArray.push(array[i]);return nArray;}; Array.prototype.each=function(func){ for(var i=0,l=this.length;i<l;i++) {func(this[i],i);}; }; document .getElementsByClassName=function(cn){ var hasClass=function(w,Name){ var hasClass = false; w.className.split(' ').each(function(s){ if (s == Name) hasClass = true; }); return hasClass; }; var elems =document.getElementsByTagName("*")||document.all; var elemList = []; $c(elems).each(function(e){ if(hasClass(e,cn)){elemList.push(e);} }) return $c(elemList); }; function change_bg(obj){ var a=document.getElementsByClassName("nav")[0].getElementsByTagName("a"); for(var i=0;i<a.length;i++){a[i].className="";} obj.className="current"; } </script> </head> <body> <p class="nav"> <UL> <LI><A href="#" onclick="change_bg(this)">脚本首页</A></LI> <LI><A href="#" onclick="change_bg(this)">网页特效</A> </LI> <LI><a href="#" class="current" onclick="change_bg(this)">精品脚本</a> </LI> <LI><A href="#" onclick="change_bg(this)">ASP代码</A> </LI> <LI><A href="#" onclick="change_bg(this)">PHP代码</A> </LI> <LI><A href="#" onclick="change_bg(this)">JSP代码</A> </LI> <LI><A href="#" onclick="change_bg(this)">脚本资源</A> </LI> <LI><A href="#" onclick="change_bg(this)">软件下载</A> </LI> </UL> </p> </body> </html>
The above is the detailed content of How does JavaScript achieve the effect of highlighting the current menu in the navigation bar when it is selected?. For more information, please follow other related articles on the PHP Chinese website!

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

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 CS6
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
