function getChildrenJSON($elem) {
    const result = []
    const $children = $elem.childNodes() || [] // 注意 childNodes() 可以获取文本节点
    $children.forEach(curElem => {
        let elemResult
        const nodeType = curElem.nodeType

        // 文本节点
        if (nodeType === 3) {
            elemResult = curElem.textContent
            elemResult = replaceHtmlSymbol(elemResult)
        }

        // 普通 DOM 节点
        if (nodeType === 1) {
            elemResult = {}

            // tag
            elemResult.tag = curElem.nodeName.toLowerCase()
            // attr
            const attrData = []
            const attrList = curElem.attributes || {}
            const attrListLength = attrList.length || 0
            for (let i = 0; i < attrListLength; i++) {
                const attr = attrList[i]
                attrData.push({
                    name: attr.name,
                    value: attr.value
                })
            }
            elemResult.attrs = attrData
            // children（递归）
            elemResult.children = getChildrenJSON($(curElem))
        }

        result.push(elemResult)
    })
    return result
}
function getChildrenJSON($elem) {
    const result = []
    const $children = $elem.childNodes() || [] // 注意 childNodes() 可以获取文本节点
    $children.forEach(curElem => {
        let elemResult
        const nodeType = curElem.nodeType

        // 文本节点
        if (nodeType === 3) {
            elemResult = curElem.textContent
            elemResult = replaceHtmlSymbol(elemResult)
        }

        // 普通 DOM 节点
        if (nodeType === 1) {
            elemResult = {}

            // tag
            elemResult.tag = curElem.nodeName.toLowerCase()
            // attr
            const attrData = []
            const attrList = curElem.attributes || {}
            const attrListLength = attrList.length || 0
            for (let i = 0; i < attrListLength; i++) {
                const attr = attrList[i]
                attrData.push({
                    name: attr.name,
                    value: attr.value
                })
            }
            elemResult.attrs = attrData
            // children（递归）
            elemResult.children = getChildrenJSON($(curElem))
        }

        result.push(elemResult)
    })
    return result
}
function getChildrenJSON ( $elem ) { const result = [ ] const $children = $elem . childNodes ( ) || [ ] // 注意 childNodes() 可以获取文本节点 $children . forEach ( curElem => { let elemResult const nodeType = curElem . nodeType // 文本节点 if ( nodeType === 3 ) { elemResult = curElem . textContent elemResult = replaceHtmlSymbol ( elemResult ) } // 普通 DOM 节点 if ( nodeType === 1 ) { elemResult = { } // tag elemResult . tag = curElem . nodeName . toLowerCase ( ) // attr const attrData = [ ] const attrList = curElem . attributes || { } const attrListLength = attrList . length || 0 for ( let i = 0 ; i < attrListLength ; i ++ ) { const attr = attrList [ i ] attrData . push ( { name : attr . name , value : attr . value } ) } elemResult . attrs = attrData // children（递归） elemResult . children = getChildrenJSON ( $ ( curElem ) ) } result . push ( elemResult ) } ) return result }
获取一个 elem.childNodes 的 JSON 数据
获取一个 elem . childNodes 的 JSON 数据
getChildrenJSON - 获取文本节点