js realizes the non-select drop-down box version of the three-level linkage of provinces, cities and districts

js realizes the non-select drop-down box version of the three-level linkage of provinces, cities and districts

I searched for three-level linkage on the Internet and found that they were all written in option. I suddenly had an idea to write one in other ways. I feel that the effect is not bad. If you are interested, you can take a look. Without further ado, let's see the effect

The code is as follows. It's a bit messy. If you want to see it, just take a look.

1.html code

<div class="box">
        <section class="province">Province</section>
        <section class="city">City</section>
        <section class="area">Area</section>
 
        <div class="si">
        </div>
</div>

2.css code

<style>
        .box{
            width: 800px;
            height: 50px;
            margin: 20px auto;
            background-color: rgb(48, 49, 48);
            border-radius: 10px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .box section{
            display: inline-block;
            background-color: rgb(168, 165, 165);
            flex-grow: 1;
            height: 30px;
            margin-right: 10px;
            margin-left: 10px;
            border-radius: 5px;
            line-height: 30px;
            padding-left: 10px;
        }
        .box section:hover{
            cursor: pointer;
        }
        .si{
            display: none;
        }
        .box .all{
            display: block;
            width: 740px;
            background-color: rgb(211, 203, 203);
            padding: 20px;
            position: absolute;
            border-radius: 10px;
            top: 57px;
        }
        .box .all:hover{
            cursor: pointer;
        }
        .box .all span{
            display: inline-block;
            width: 130px;
            height: 30px;
            font-size: 13px;
            padding-left: 10px;
            line-height: 30px;
            border-radius: 5px;
            margin-left: 10px;
            border: 1px solid #000;
            background-color: white;
            box-sizing: border-box;
            margin-top: 10px;
        }
</style>

3.js code

<script>
        // Get content var data = city_code // This is my data // Get province, city, district var province = document.querySelector(".province")
        var city = document.querySelector(".city")
        var area = document.querySelector(".area")
 
        // Get the hidden div
        var si = document.querySelector(".si")
        
        province.addEventListener("mouseover",function(){
            si.classList.add("all")
            // Select the province var html = ""
            var all = document.querySelector(".all")
            for(var i = 0;i<data.length;i++){
                const provinceName = data[i].name
                const provinceID = data[i].code
                html += `<span id="${provinceID}">${provinceName}</span>`
            }
            all.innerHTML=html
 
            var spanAll = document.querySelectorAll("span")
            for(var j =0;j<spanAll.length;j++){
                spanAll[j].addEventListener("click",function(){
                    province.innerHTML=this.innerText
                    province.id=this.id
 
                    html = ""
 
                    // Select a city for(var k = 0;k<data.length;k++){
                if (data[k].code===province.id) {
                    var citys = data[k].city
                    for(var i = 0;i<citys.length;i++){
                    html +=`<span id="${citys[i].code}">${citys[i].name}</span>`
                    }
                    all.innerHTML=html
                    var spanAll = document.querySelectorAll("span")
                    for(var j =0;j<spanAll.length;j++){
                        spanAll[j].addEventListener("click",function(){
                        city.innerHTML=this.innerText
                        city.id=this.id
 
                        html = ""
 
                        //Select area for(var k = 0; k<citys.length; k++){
                if (citys[k].code===city.id) {
                    var areas = cities[k].area
                    for(var i = 0;i<areas.length;i++){
                    html +=`<span id="${areas[i].code}">${areas[i].name}</span>`
                    }
                    all.innerHTML=html
                    var spanAll = document.querySelectorAll("span")
                    for(var j =0;j<spanAll.length;j++){
                        spanAll[j].addEventListener("click",function(){
                        area.innerHTML = this.innerText
                        area.id=this.id
                        si.classList.remove("all")                 
                        })               
                    }
                    break
                }
            }
 
                        })                
                    }
                    break
                }
            }    
 
 
                         
                })                
            }
            
 
        })
 
</script>

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:
  • PHP+Mysql+Ajax+JS realizes the linkage between provinces, cities and districts
  • JavaScript realizes the three-level linkage drop-down box menu of provinces, cities and districts
  • Province, city and district three-level linkage drop-down box menu javascript version
  • js realizes a three-level linkage selection box code sharing of provinces, cities and districts
  • Pure JS provincial, municipal and district three-level linkage menu extracted from the QQ website
  • JSON+Jquery provincial, municipal and district three-level linkage
  • Native js realizes the three-level linkage code sharing of provinces, cities and districts
  • Vue.js imitates the selection component example code of Jingdong's three-level linkage of provinces, cities and districts
  • JavaScript realizes simple three-level linkage between provinces, cities and districts
  • JavaScript provincial, municipal and district three-level linkage drop-down menu example demonstration

<<:  Summary of several situations in which MySQL indexes fail

>>:  How to configure Tomcat and run your first Java Web project on IntelliJ IDEA 2018

Recommend

In-depth understanding of the vertical-align property and baseline issues in CSS

vertical-align attribute is mainly used to change...

Using JS to implement binary tree traversal algorithm example code

Table of contents Preface 1. Binary Tree 1.1. Tra...

The three new indexes added in MySQL 8 are hidden, descending, and functions

Table of contents Hidden, descending, and functio...

MySQL Failover Notes: Application-Aware Design Detailed Explanation

1. Introduction As we all know, in the applicatio...

Linux debugging tools that developers and operators must look at [Recommended]

System performance expert Brendan D. Gregg update...

Example analysis of the usage of the new json field type in mysql5.7

This article uses an example to illustrate the us...

Detailed explanation of HTML page header code example

Knowledge point 1: Set the base URL of the web pa...

Using vsftp to build an FTP server under Linux (with parameter description)

introduce This chapter mainly introduces the proc...

Working principle and implementation method of Vue instruction

Introduction to Vue The current era of big front-...

HTML table tag tutorial (26): cell tag

The attributes of the <TD> tag are used to ...

The difference and usage of Ctrl+z, Ctrl+c and Ctrl+d in Linux commands

What does Ctrl+c, Ctrl+d, Ctrl+z mean in Linux? C...