案例28-无限极菜单-递归


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>无限极菜单</title>

    <style>

        body {

            margin: 0;

            background: url(./img/bg.png);

            min-height: 100vh;

            text-indent: 10px;

        }

        ul span{

            text-indent: 0;

        }

        h1 {

            margin: 40px auto;

            background: url(./img/logo.png);

            width: 208px;

            height: 56px;

            text-indent: -9999px;

        }

        ul {

            padding: 0;

            margin: 0 0 0 100px;

            list-style: none;

            /* display: none; */

        }

        .list-wrap>ul {

            display: block;

        }

        li {

            line-height: 30px;

            cursor: pointer;

            border-left: 1px dotted #e15671;

            position: relative;

        }

        .last{

            border-left:none;

        }

        span {

            padding: 0 10px;

            margin-left: -15px;

            color: #e15671;

            position: absolute;

            left: 0;

            top: 0;

        }

        h4{

            font-weight: normal;

            margin: 0;

        }


    </style>

</head>

<body>

    <h1>miaov</h1>

    <div class="list-wrap">

        <!-- <ul>

            <li>

                <span>+</span>

                <h4>第一阶段 JS基础入门</h4>

                <ul>

                    <li>

                        <span>+</span>

                        第一章

                        <ul>

                            <li class="last">JS简介</li>

                            <li class="last">JS简介</li>

                            <li class="last">JS简介</li>

                        </ul>

                    </li>

                    <li>

                        <span>+</span>

                        第二章

                    </li>

                </ul>

            </li>

            <li class="last">

                <h4>第二阶段 JS核心功能探秘</h4>

            </li>

        </ul> -->

    </div>

    <script src="./data.js"></script>

    <script>

        var txt = loop(datas)

        function loop(arr){

            var str = '';

            for (var i = 0; i < arr.length; i++) {

                if (!!arr[i].children) {

                    // console.log('有嵌套的');

                    var vv = loop(arr[i].children);

                    str += '<li><span>+</span>'+arr[i].name+ vv +'</li>'

                } else {

                    // console.log(arr[i]);

                    str += '<li class="last">'+ arr[i].name+'</li>'

                }

            }

            if(str){

                str = '<ul>' + str + '</ul>'

            }

            return str;

            // console.log(str);


        }

        console.log(txt);

        var listWarp = document.querySelector('.list-wrap');

        listWarp.innerHTML = txt;

    </script>

</body>

</html>

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容