Four ways to implement tab switching in javascript
This article mainly introduces four ways to implement tab switching in JavaScript, and evaluates each method. Interested friends can refer to it
Tab switching is very common in web pages. Therefore, 4 implementation methods have been summarized recently.
First, write the tab frame and add the simplest style. The code is as follows:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style> *{ padding: 0; margin: 0; } li{ list-style: none; float:left; } #tabCon{ clear: both; } </style> </head> <body> <p id="tanContainer"> <p id="tab"> <ul> <li>标题一</li> <li>标题二</li> <li>标题三</li> <li>标题四</li> </ul> </p> <p id="tabCon"> <p>内容一</p> <p>内容二</p> <p>内容三</p> <p>内容四</p> </p> </p> </body> </html>
The current display effect is as follows:
The four tab titles and four content areas are all displayed on the page. Now we need to achieve the tab switching effect, that is, click title one, content one will be displayed, and other content will be displayed. Not displayed; click title two, content two is displayed, and other content is not displayed...
Then, the overall idea is very simple, bind events to four titles, and when triggered, the corresponding content is displayed, and other content is hidden.
Method 1: The content corresponding to the clicked title is displayed, and the content corresponding to the non-clicked title is hidden. The code is as follows:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style> *{ padding: 0; margin: 0; } li{ list-style: none; } </style> <script> function tab(pid){ var tabs=["tab1","tab2","tab3","tab4"]; for(var i=0;i<4;i++){ if(tabs[i]==pid){ document.getElementById(tabs[i]).style.display="block"; }else{ document.getElementById(tabs[i]).style.display="none"; } } } </script> </head> <body> <p id="tanContainer"> <p id="tabNav"> <ul> <li onclick="tab('tab1')">标题一</li> <li onclick="tab('tab2')">标题二</li> <li onclick="tab('tab3')">标题三</li> <li onclick="tab('tab4')">标题四</li> </ul> </p> <p id="tab"> <p id="tab1">内容一</p> <p id="tab2">内容二</p> <p id="tab3">内容三</p> <p id="tab4">内容四</p> </p> </p> </body> </html>
##Method 2: First set all content to be hidden, then click on the title pair The content used is displayed. The code is as follows:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style> *{ padding: 0; margin: 0; } li{ list-style: none; float:left; } #tabCon{ clear: both; } #tabCon_1{ display: none; } #tabCon_2{ display: none; } #tabCon_3{ display: none; } </style> <script> function changeTab(tabCon_num){ for(i=0;i<=3;i++) { document.getElementById("tabCon_"+i).style.display="none"; //将所有的层都隐藏 } document.getElementById("tabCon_"+tabCon_num).style.display="block";//显示当前层 } </script> </head> <body> <p id="tanContainer"> <p id="tab"> <ul> <li onclick="changeTab('0')">标题一</li> <li onclick="changeTab('1')">标题二</li> <li onclick="changeTab('2')">标题三</li> <li onclick="changeTab('3')">标题四</li> </ul> </p> <p id="tabCon"> <p id="tabCon_0">内容一</p> <p id="tabCon_1">内容二</p> <p id="tabCon_2">内容三</p> <p id="tabCon_3">内容四</p> </p> </p> </body> </html>
##Method 3: Show and hide through class control. First, set the hidden display of all content to none, and set the display of the class to block. Traverse all title nodes and content nodes. After clicking the title, the title node and the corresponding content node have the class, and the others do not. The code is as follows:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style> *{ padding: 0; margin: 0; } li{ list-style: none; float:left; } #tabCon { clear: both; } #tabCon p { display:none; } #tabCon p.fp { display:block; } </style> </head> <body> <p id="tanContainer"> <p id="tab"> <ul> <li class="fli">标题一</li> <li>标题二</li> <li>标题三</li> <li>标题四</li> </ul> </p> <p id="tabCon"> <p class="fp">内容一</p> <p>内容二</p> <p>内容三</p> <p>内容四</p> </p> </p> </body> <script> var tabs=document.getElementById("tab").getElementsByTagName("li"); var ps=document.getElementById("tabCon").getElementsByTagName("p"); for(var i=0;i<tabs.length;i++){ tabs[i].onclick=function(){change(this);} } function change(obj){ for(var i=0;i<tabs.length;i++){ if(tabs[i]==obj){ tabs[i].className="fli"; ps[i].className="fp"; }else{ tabs[i].className=""; ps[i].className=""; } } } </script> </html>
The disadvantage of this method is that there can no longer be a p tag under the p of the content block.
Method 4: Instead of js, use "input:checked" to switch tabs. First hide all content and display the selected content. The code is as follows:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>input:checked实现tab切换</title> <style> input{ opacity: 0;/*隐藏input的选择框*/ } label{ cursor: pointer;/*鼠标移上去变成手状*/ float: left; } label:hover{ background: #eee; } input:checked+label{ color: red; } /*选择前面有.tabs input:nth-of-type(x):checked的.panels .panel:nth-child(x)*/ .tabs input:nth-of-type(1):checked~.panels .panel:nth-child(1), .tabs input:nth-of-type(2):checked~.panels .panel:nth-child(2){ opacity: 1; } .panel{ opacity: 0; position: absolute;/*使内容区域位置一样*/ } </style> </head> <body> <p class="tabs"> <input checked id="one" name="tabs" type="radio"> <label for="one">标题一</label> <input id="two" name="tabs" type="radio"> <label for="two">标题二</label> <p class="panels"> <p class="panel"> <p>内容一</p> </p> <p class="panel"> <p>内容二</p> </p> </p> </p> </body> </html>
The disadvantage of this method is that switching between different areas can only be done by clicking.
The above is the tab switching implementation method summarized for everyone. I hope it will be helpful to everyone's learning. Follow this idea and start making your own tab switching special effects.
For more related articles on the four methods of tab switching using javascript, please pay attention to the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

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.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.
