列表

详情


FED92. dom节点转成json数据

描述

页面上存在id=jsContainer的节点A,系统会随机在节点A中生成文档片段,请按照如下需求实现 dom2json 函数
1、dom2json需要分析整个节点A的dom结构,并将其结构转换为对应的json对象
2、需要获取dom结构的标签名称(tag),所有属性(attributes),子节点(children)
3、文档片段中的属性形式均为 name="value",解析之后的格式为{name: value}, 属性值为String类型,不需要做解析
4、随机生成的文档片段中,只包含 nodeType 为1(element)和3(text)的节点,不需要考虑其他节点类型
5、纯文本也视为一个节点, json格式为 {tag: 'text', content: '文本内容'},content为文本内容执行trim后的结果,如果该结果为空,则忽略当前节点
6、返回结果中的标签名称不区分大小写
7、如果节点不包含属性值或者子节点,其对应的结果中需要保留attributes以及children字段,例如 {tag: 'div', attributes: {}, children: []}
8、当前界面执行dom2json之后的结果为如下图所示
9、请不要手动修改html和css
10、不要使用第三方插件

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 958ms, 内存消耗: 77848KB, 提交时间: 2021-06-30

{"css":"body{\n    margin: 0;\n    font-size: 14px;\n}","js":"        function dom2json() {\n            let root = document.getElementById('jsContainer');\n            return JSON.parse(JSON.stringify(analysisDom(root)))\n        }\n        function analysisDom(dom) {\n            let templete;\n            if (dom.nodeType == 1) {\n                templete = {\n                    tag: '',\n                    attributes: {},\n                    children: []\n                };\n                templete.tag = dom.localName;\n                let len = dom.attributes.length;\n                for (let i = 0; i < len; i++) {\n                    templete.attributes[dom.attributes.item(i).nodeName] = dom.getAttribute(dom.attributes.item(i).nodeName);\n                };\n                let cLen = dom.childNodes.length\n                if (cLen) {\n                    for (let i = 0; i < cLen; i++) {\n                        if(analysisDom(dom.childNodes[i])){\n                            templete.children.push(analysisDom(dom.childNodes[i]))\n                        }\n                    }\n                }\n            } else if (dom.nodeType == 3) {\n                let data = dom.textContent.trim();\n                if (data) {\n                    templete = {\n                        tag: 'text',\n                        content: data\n                    }\n                }\n            }\n            return templete\n        }","html":"<div id=\"jsContainer\">\n    <ul class=\"js-test\" id=\"jsParent\">\n        <li data-index=\"0\">1</li>\n        <li data-index=\"1\">2</li>\n    </ul>\n    <span style=\"font-weight: bold;\">3</span>\n    4\n</div>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 959ms, 内存消耗: 77848KB, 提交时间: 2021-06-30

{"css":"body{\n    margin: 0;\n    font-size: 14px;\n}","js":"function dom2json() {\n    const jsContainer = document.querySelector(\"#jsContainer\")\n\n    function domJson(dom) {\n        var obj = {\n            tag: getTagName(dom)\n        }\n        if (dom.nodeType == 1) {\n            var attrs = getTagAttrs(dom)\n            if (attrs) obj.attributes = attrs;\n            obj.children = Array.from(dom.childNodes).filter(child => {\n                return !(child.nodeType == 3 && !child.textContent.trim())\n            }).map(child => domJson(child))\n            return obj\n        }\n        if (dom.nodeType == 3) {\n            obj.content = texthandle(dom.textContent)\n            return obj\n        }\n    }\n\n    function texthandle(str) {\n        return str.replace(/\\s/g,'')\n    }\n\n    function getTagName(dom) {\n        return dom.nodeName.toLocaleLowerCase().replace('#', '')\n    }\n\n    function getTagAttrs(dom) {\n        var attr = Array.from(dom.attributes)\n        var obj = {}\n        attr.forEach(atr => obj[atr.name] = atr.value)\n        return attr.length ? obj : null;\n    }\n\n    return domJson(jsContainer)\n}","html":"<div id=\"jsContainer\">\n    <ul class=\"js-test\" id=\"jsParent\">\n        <li data-index=\"0\">1</li>\n        <li data-index=\"1\">2</li>\n    </ul>\n    <span style=\"font-weight: bold;\">3</span>\n    4\n</div>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 963ms, 内存消耗: 77872KB, 提交时间: 2021-06-21

{"css":"body{\n    margin: 0;\n    font-size: 14px;\n}","js":"function dom2json() {\n    // 找到容器\n    const jsContainer = document.getElementById(\"jsContainer\");\n    // 将节点转化为json\n    function handleDom(node) {\n        // 虚拟节点\n        let vnode = {\n            tag: node.tagName.toLowerCase(),\n            attributes: {},\n            children: [],\n        };\n        // 属性和子节点\n        let attributes = node.attributes,\n            childNodes = node.childNodes;\n        // 遍历节点属性\n        if (attributes.length > 0) {\n            for (let i = 0; i < attributes.length; i++) {\n                let item = attributes[i];\n                vnode.attributes[item.nodeName] = item.nodeValue;\n            }\n        }\n        // 遍历子节点\n        if (childNodes.length > 0) {\n            for (let i = 0; i < childNodes.length; i++) {\n                let item = childNodes[i];\n                if (item.nodeType === Node.TEXT_NODE) {\n                    // 属性节点\n                    if (item.textContent.trim()) {\n                        let kids = {\n                            tag: \"text\",\n                            content: item.textContent,\n                        };\n                        vnode.children.push(kids);\n                    }\n                } else if (item.nodeType === Node.ELEMENT_NODE) {\n                    // 元素节点,递归处理\n                    vnode.children.push(handleDom(item));\n                }\n            }\n        }\n        return vnode;\n    }\n    return handleDom(jsContainer);\n}","html":"<div id=\"jsContainer\">\n    <ul class=\"js-test\" id=\"jsParent\">\n        <li data-index=\"0\">1</li>\n        <li data-index=\"1\">2</li>\n    </ul>\n    <span style=\"font-weight: bold;\">3</span>\n    4\n</div>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 966ms, 内存消耗: 77872KB, 提交时间: 2021-06-22

{"css":"body{\n    margin: 0;\n    font-size: 14px;\n}","js":"function dom2json() {\n    // 找到容器\n    const jsContainer = document.getElementById(\"jsContainer\");\n    // 将节点转化为json\n    function parseDom(node) {\n        // 虚拟节点\n        let obj = {\n            tag: node.tagName.toLowerCase(),\n            attributes: {},\n            children: [],\n        };\n        // 属性和子节点\n        let attributes = node.attributes,\n            childNodes = node.childNodes;\n        // 遍历节点属性\n        if (attributes.length > 0) {\n            for (let i = 0; i < attributes.length; i++) {\n                let item = attributes[i];\n                obj.attributes[item.nodeName] = item.nodeValue;\n            }\n        }\n        // 遍历子节点\n        if (childNodes.length > 0) {\n            for (let i = 0; i < childNodes.length; i++) {\n                let item = childNodes[i];\n                if (item.nodeType === 3) {\n                    // 属性节点\n                    if (item.textContent.trim()) {\n                        let kids = {\n                            tag: \"text\",\n                            content: item.textContent,\n                        };\n                        obj.children.push(kids);\n                    }\n                } else if (item.nodeType === 1) {\n                    // 元素节点,递归处理\n                    obj.children.push(parseDom(item));\n                }\n            }\n        }\n        return obj;\n    }\n    return parseDom(jsContainer);\n}","html":"<div id=\"jsContainer\">\n    <ul class=\"js-test\" id=\"jsParent\">\n        <li data-index=\"0\">1</li>\n        <li data-index=\"1\">2</li>\n    </ul>\n    <span style=\"font-weight: bold;\">3</span>\n    4\n</div>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 981ms, 内存消耗: 77864KB, 提交时间: 2021-07-13

{"css":"body{\n    margin: 0;\n    font-size: 14px;\n}","js":"function dom2json() {\n            const jsContainer = document.querySelector(\"#jsContainer\")\n \n            function domJson(dom) {\n                var obj = {\n                    tag: getTagName(dom)\n                }\n                if (dom.nodeType == 1) {\n                    var attrs = getTagAttrs(dom)\n                    if (attrs) obj.attributes = attrs;\n                    console.log(dom.children)\n                    obj.children = Array.from(dom.childNodes).filter(child => {\n                        return !(child.nodeType == 3 && !child.textContent.trim())\n                    }).map(child => domJson(child))\n                    return obj\n                }\n                if (dom.nodeType == 3) {\n                    obj.content = texthandle(dom.textContent)\n                    return obj\n                }\n            }\n \n            function texthandle(str) {\n                return str.replace(/\\s/g, '')\n            }\n \n            function getTagName(dom) {\n                return dom.nodeName.toLocaleLowerCase().replace('#', '')\n            }\n \n            function getTagAttrs(dom) {\n                var attr = Array.from(dom.attributes)\n                var obj = {}\n                attr.forEach(atr => obj[atr.name] = atr.value)\n                return attr.length ? obj : null;\n            }\n \n            return domJson(jsContainer)\n        }","html":"<div id=\"jsContainer\">\n    <ul class=\"js-test\" id=\"jsParent\">\n        <li data-index=\"0\">1</li>\n        <li data-index=\"1\">2</li>\n    </ul>\n    <span style=\"font-weight: bold;\">3</span>\n    4\n</div>","libs":[]}

上一题