遍历Dom数

function traverse(element)
{
/**
* 获得节点
*/
var node = element || document;

    /**
     * 设置指针
     */
    var focusNode  = node.firstChild;
    
    /**
     * 循环遍历
     */
    while (focusNode != null) 
    {
        if (focusNode.nodeType == 1) 
        {
            
            //递归 
            traverse(focusNode);
            
            console.log(focusNode);
        }
        
        //遍历子节点
        focusNode = focusNode.nextSibling;   
    }
}


traverse(document.body)

你可能感兴趣的:(遍历Dom数)