凡尘堕世 2021-04-29 10:17 采纳率: 0%
浏览 36

代码有点问题,不知道怎么改,点城市获取不到区县

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 450px;
            padding: 5px;
            border: 2px solid #eeeeee;
        }
        .xx{
            border-bottom: 2px solid #56b4f8;
            list-style: none;
            width: 450px;
            height: 40px;
            box-sizing: border-box;
        }
        .xx li{
            width: 80px;
            float: left;
            height: 40px;
            line-height: 40px;
            text-align: center;
        }
        .active{
            background-color:  #56b4f8;
            color: #f5f5f5;
        }
        span{
            width: 80px;
            height: 30px;
            display: inline-block;
            font-size: 12px;
            text-align: center;
            line-height: 15px;
            margin: 5px;
            /* float: left; */
        }
        span:hover{
            background-color:  #56b4f8;
            color: #f5f5f5;
        }
        #country{
            width: 450px;
            /* height: 300px; */
            background-color: pink;
        }
        #countryside{
            width: 450px;
            /* height: 300px; */
            background-color: yellow;
        }
        .sure{
            background-color:  #56b4f8;
            color: #f5f5f5;
        }
    </style>
</head>
<body>
    <div class="box">
        <ul class="xx">
            <li class="l1 active">省份</li>
            <li class="l1">城市</li>
            <li class="l1">区县</li>
        </ul>
        <div class="show">
            <div id="city"></div>
            <div id="country"></div>
            <div id="countryside"></div>
        </div>
    </div>
    <script src="./area-json.js"></script>
    <script>
        var li = document.querySelectorAll(".xx li")
        var div = document.querySelectorAll(".show div")
        var box = document.querySelector(".box")       
        var show = document.querySelector(".show")
        var city = document.getElementById("city");
        var country = document.getElementById("country");
        var countryside = document.getElementById("countryside");
        var countryarr = [];
        var countrysidearr = [];
        add(city,data)
        for(var i = 0;i<data.length;i++){
            city.children[i].onclick = function(){
                country.innerText= ""
                for(var j = 0;j<data.length;j++){
                    if(data[j].code == this.className){
                        countryarr = data[j].children        
                    }
                }

                for(var d = 0;d<li.length;d++){
                    li[d].className = "li"
                }
                li[1].className = "l1 active"
                div[1].style.display = "block"
                div[0].style.display = "none"
                div[2].style.display = "none"

                add(country,countryarr)
            }
        }
        for(var k = 0;k<countryarr.length;k++){
            country.children[k].onclick = function(){
                countryside.innerHTML = ""
                for(var l = 0;l<data.length;l++){
                    if(data[l].code == this.className){
                        countrysidearr = countryarr[j].children        
                    }
                }
                add(countryside,countrysidearr)
            }
        }
        li[0].onclick = function(){
            for(var b = 0;b<li.length;b++){
                li[b].className = "li"
            }
            li[0].className = "l1 active"
            div[0].style.display = "block"
            div[1].style.display = "none"
            div[2].style.display = "none"
        }
        li[1].onclick = function(){
            for(var b = 0;b<li.length;b++){
                li[b].className = "li"
            }
            li[1].className = "l1 active"
            div[1].style.display = "block"
            div[0].style.display = "none"
            div[2].style.display = "none"
        }
        li[2].onclick = function(){
            for(var b = 0;b<li.length;b++){
                li[b].className = "li"
            }
            li[2].className = "l1 active"
            div[2].style.display = "block"
            div[0].style.display = "none"
            div[1].style.display = "none"
        }        
        function add (dom,arr){
            for(var i = 0;i<arr.length;i++){
                var span = document.createElement("span");
                span.innerHTML = arr[i].name;
                span.className = arr[i].code;
                dom.appendChild(span);
            }
        }
    </script>
</body>
</html>

js文件下载地址:https://download.csdn.net/download/weixin_46073353/16759489?spm=1001.2014.3001.5503

  • 写回答

1条回答 默认 最新

  • 菜鸟咸鱼一锅端 2021-04-29 11:45
    关注

    countrysidearr的值置空了

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            .box{
                width: 450px;
                padding: 5px;
                border: 2px solid #eeeeee;
            }
            .xx{
                border-bottom: 2px solid #56b4f8;
                list-style: none;
                width: 450px;
                height: 40px;
                box-sizing: border-box;
            }
            .xx li{
                width: 80px;
                float: left;
                height: 40px;
                line-height: 40px;
                text-align: center;
            }
            .active{
                background-color:  #56b4f8;
                color: #f5f5f5;
            }
            span{
                width: 80px;
                height: 30px;
                display: inline-block;
                font-size: 12px;
                text-align: center;
                line-height: 15px;
                margin: 5px;
                /* float: left; */
            }
            span:hover{
                background-color:  #56b4f8;
                color: #f5f5f5;
            }
            #country{
                width: 450px;
                /* height: 300px; */
                background-color: pink;
            }
            #countryside{
                width: 450px;
                /* height: 300px; */
                background-color: yellow;
            }
            .sure{
                background-color:  #56b4f8;
                color: #f5f5f5;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <ul class="xx">
                <li class="l1 active">省份</li>
                <li class="l1">城市</li>
                <li class="l1">区县</li>
            </ul>
            <div class="show">
                <div id="city"></div>
                <div id="country"></div>
                <div id="countryside"></div>
            </div>
        </div>
        <script src="./area.json"></script>//这里是我这边的数据改成你的
        <script>
            var li = document.querySelectorAll(".xx li")
            var div = document.querySelectorAll(".show div")
            var box = document.querySelector(".box")       
            var show = document.querySelector(".show")
            var city = document.getElementById("city");
            var country = document.getElementById("country");
            var countryside = document.getElementById("countryside");
            var countryarr = [];
            var countrysidearr = [];
            var data = data.data;//这里是我这边的数据,改成你的
            console.log('这是什么?',data)
            add(city,data)
            for(var i = 0;i<data.length;i++){
                city.children[i].onclick = function(){
                    country.innerText= ""
                    for(var j = 0;j<data.length;j++){
                        if(data[j].code == this.className){
                            countryarr = data[j].children        
                        }
                    }
                    for(var d = 0;d<li.length;d++){
                        li[d].className = "li"
                    }
                    li[1].className = "l1 active"
                    div[1].style.display = "block"
                    div[0].style.display = "none"
                    div[2].style.display = "none"
                    add(country,countryarr)
                    for(var k = 0;k<countryarr.length;k++){
                        console.log('进来了吗',countryarr.length)
                        country.children[k].onclick = function(){
                            countryside.innerHTML = ""
                            for(var n=0;n<countryarr.length;n++){
                                // console.log('ddd',countryarr[n])
                                if(countryarr[n].code == this.className){
                                    countrysidearr = countryarr[n].children
                                    console.log('区县',countrysidearr)        
                                }
                            } 
                            for(var d = 0;d<li.length;d++){
                                li[d].className = "li"
                            }
                            li[2].className = "l1 active"
                            div[1].style.display = "none"
                            div[0].style.display = "none"
                            div[2].style.display = "block" 
                            add(countryside,countrysidearr)
                        }
                    }
                }
            }
            
            li[0].onclick = function(){
                for(var b = 0;b<li.length;b++){
                    li[b].className = "li"
                }
                li[0].className = "l1 active"
                div[0].style.display = "block"
                div[1].style.display = "none"
                div[2].style.display = "none"
            }
            li[1].onclick = function(){
                for(var b = 0;b<li.length;b++){
                    li[b].className = "li"
                }
                li[1].className = "l1 active"
                div[1].style.display = "block"
                div[0].style.display = "none"
                div[2].style.display = "none"
            }
            li[2].onclick = function(){
                for(var b = 0;b<li.length;b++){
                    li[b].className = "li"
                }
                li[2].className = "l1 active"
                div[2].style.display = "block"
                div[0].style.display = "none"
                div[1].style.display = "none"
            }        
            function add (dom,arr){
                for(var i = 0;i<arr.length;i++){
                    var span = document.createElement("span");
                    span.innerHTML = arr[i].name;
                    span.className = arr[i].code;
                    dom.appendChild(span);
                }
            }
        </script>
    </body>
    </html>
    评论

报告相同问题?

悬赏问题

  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥50 如何将脑的图像投影到颅骨上
  • ¥15 提问一个关于vscode相关的环境配置问题,就是输入中文但是显示不出来,代码在idea可以显示中文,但在vscode不行,不知道怎么配置环境
  • ¥15 netcore使用PuppeteerSharp截图
  • ¥20 这张图页头,页脚具体代码该怎么写?
  • ¥15 关于#sql#的问题,请各位专家解答!
  • ¥20 WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
  • ¥15 数字信号处理考试111