遍历DOM打平

html 模板

 <div class="box">
        <p>1</p>
        <p>2</p>
        <div>
            <p>a</p>
            <p>b</p>
        </div>
        <div>
            <p>x</p>
            <p>y</p>
            <div>
                <span>asd</span>
            </div>
        </div>
    </div>

js

let l = console.log
        let box = document.querySelector('.box')
        let toArray = Array.from;

        scan(box)

        function scan(box) {
            let arr = toArray(box.children)
            for (let ele of arr) {
                if (ele.children.length) {
                   scan(ele)
                }else{
                    l( ele)
                }
            }
        }
posted @ 2018-06-23 19:45  Ajanuw  阅读(181)  评论(0编辑  收藏  举报