JavaScript to implement simple tab bar switching content bar

JavaScript to implement simple tab bar switching content bar

This article shares the specific code of JavaScript to achieve simple tab bar switching content bar for your reference. The specific content is as follows

HTML+CSS part

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* Initialize css, because there is a default margin*{ margin:0; padding:0;}

h1,h2,h3,h4,h5,h6{font-size:100%; font-weight:normal;}

ol,ul{ list-style:none;}

img,fieldset{ border:0 none; display:block;} */
        div,ul,li{
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        ul{
            list-style-type: none;
        }
        a{
            text-decoration: none;
        }
        #nav{
            width: 450px;
            height: 400px;
            margin: 100px auto;/*center left and right*/
            background-color: pink;
            padding: 0;
            font-size: 14px;
        }
        
        li{
            float: left;
            width: 150px;
            height: 30px;
            text-align: center;
            line-height: 0.6rem;
            border: 0.02rem solid #ccc;
        }
        .content{
            clear: both;
            position: relative;
        }
        .content div{
            width: 450px;
            height: 370px;
            position: absolute;
            display: none;
        }
        .tab .choose{
            background-color: skyblue;
        }
        .content .current{
            display: block;
        }
    </style>
    <script src="jQuery.mini.js"></script>
   
</head>
<body>
    <div id="nav">
        <div class="tab">
            <ul>
                <li class="choose" ><a href="#" > 1</a></li>
                <li><a href="#" >2</a></li>
                <li><a href="#" >3</a></li>
            </ul>
        </div>
        <div class="content">
            <div class="current">1</div>
            <div>2</div>
            <div>3</div>
        </div>
    </div>
    </body>
</html>

Native js code

var lis = document.querySelectorAll('li');
var divs = document.querySelector('.content').querySelectorAll('div');
  for(var i=0 ; i<lis.length ; i++){
            lis[i].setAttribute('dateIndex',i);//Set the index of each li to facilitate locking the content bar later lis[i].addEventListener('click',function(){
                for(var j=0 ; j<lis.length ; j++){
                    lis[j].className = ''; //Set the style of all li to empty}
                this.className = 'choose'; //Set the style of the currently clicked li (exclusive)
                var index = this.getAttribute('dateIndex'); //Get the index of the current li
                // console.log(index);
                for(var j=0 ; j<lis.length ; j++){
                   divs[j].className = '';//Exclusive// lis[i].className = 'choose';
                }
                divs[index].className = 'current';
          })
        }
## jQuery Methods ```javascript
// jQuery method $(function(){
            $("li").click(function(){
                $(this).addClass("choose"); //Add styles to the currently clicked li $(this).siblings("li").removeClass("choose"); //Remove styles from its sibling elements (exclusive)
                var index = $(this).index(); //Get the index of the currently clicked li
                $(".content div").eq(index).show().siblings("div").hide();
                //Get the corresponding content box through index, display it through show(), and then hide it by selecting its brother element, decomposition ======》
                //$(".content div").eq(index).show();
                // $(".content div").eq(index).siblings("div").hide()
            })
        }) 

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:
  • JavaScript implements Tab bar switching effects
  • JavaScript to achieve simple tab bar switching case
  • Example of JavaScript TAB bar switching effect
  • js to achieve tab bar switching effect
  • JavaScript to achieve tab bar switching effect
  • js tab bar switching code example analysis
  • JavaScript to achieve the effect of tab bar switching

<<:  How to set Nginx to forward the domain name to the specified port

>>:  RHEL7.5 mysql 8.0.11 installation tutorial

Recommend

Implementation of Docker to build Zookeeper&Kafka cluster

I've been learning Kafka recently. When I was...

Detailed explanation of using Docker to build externally accessible MySQL

Install MySQL 8.0 docker run -p 63306:3306 -e MYS...

Analysis of MySQL crash recovery based on Redo Log and Undo Log

Table of contents MySQL crash recovery process 1....

Usage and scenario analysis of npx command in Node.js

npx usage tutorial Tonight, when I was learning V...

Google Translate Tool: Quickly implement multilingual websites

Google China has released a translation tool that ...

IDEA complete code to connect to MySQL database and perform query operations

1. Write a Mysql link setting page first package ...

Use of MySQL trigger

Table of contents 1. Trigger Introduction 1. What...

Recommended plugins and usage examples for vue unit testing

Table of contents frame First-class error reporti...

How to modify the initial password of MySQL on MAC

Problem description: I bought a Mac and installed...

MySQL DML language operation example

Additional explanation, foreign keys: Do not use ...

How to hide the version number and web page cache time in Nginx

Nginx optimization---hiding version number and we...

Window.name solves the problem of cross-domain data transmission

<br />Original text: http://research.microso...

How to deploy python crawler scripts on Linux and set up scheduled tasks

Last year, due to project needs, I wrote a crawle...