递归遍历所有ul下的所有子节点

结构:

<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>
        4
        <ul>
            <li>a</li>
            <li>b</li>
            <li>c</li>
        </ul>
    </li>
</ul>

  js:

var d  = null;
    function dg(dom) {
        for (var i  =0, len = dom.length; i < len; i++) {
            d = dom[i]
            console.log(dom[i])
            if (dom[i].children) {
                dg(dom[i].children)
            }
        }
    }
    dg(document.querySelector('ul').children)

  

原文地址:https://www.cnblogs.com/ly-qingqiu/p/12155888.html