算法二:DOM - 将DOM节点元素转换成JSON字符串

news/2024/10/19 3:26:21/

题目:
将DOM节点元素转换成JSON的格式
例如

<div class="root"><div class="child1"><p></p></div><span></span><div><div><p></p></div></div><p></p>
</div>

转换显示成(简写,大致表达个意思):
{"tagName":"DIV", className="root", childs:[{"tagName":"DIV", className="root",childs:[]}]}

乍一看上去并不是很难,递归,找出各个节点的child节点即可,可真正当场写出代码还是需要锻炼锻炼,反正我当时没写出来,课后补习:
试一试:http://jsrun.net/VPaKp/edit
HTML File:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=, initial-scale="><meta http-equiv="X-UA-Compatible" content=""><title></title>
</head>
<body><div class="root"><div class="child1"><p></p></div><span class="span1"><span></span></span><div><div><p class="p1"></p></div></div><p><span></span></p></div>
</body>
</html>

JS File:

function convertToJson() {const root = document.getElementsByClassName('root')[0];const output = new Object();output.tagName = root.tagName;output.className = root.className;output.childs = getChilds(root);console.log(JSON.stringify(output));
}function getChilds(node) {const childs = node.children;const result = new Array();if(!childs || childs.length === 0) return result;for (const child of childs) {const childOutput = new Object();childOutput.tagName = child.tagName;childOutput.className = child.className;childOutput.childs = getChilds(child);result.push(childOutput);}return result;
}
convertToJson();

运行结果:
{"tagName":"DIV","className":"root","childs":[{"tagName":"DIV","className":"child1","childs":[{"tagName":"P","className":"","childs":[]}]},{"tagName":"SPAN","className":"span1","childs":[{"tagName":"SPAN","className":"","childs":[]}]},{"tagName":"DIV","className":"","childs":[{"tagName":"DIV","className":"","childs":[{"tagName":"P","className":"p1","childs":[]}]}]},{"tagName":"P","className":"","childs":[{"tagName":"SPAN","className":"","childs":[]}]}]}

随便找了一个格式化的网站把json字符串格式化显示下:

0a16af9ed4a87fdb6e199cb5c7678df6.png

在对比下HTML文件目录:

<div class="root"><div class="child1"><p></p></div><span class="span1"><span></span></span><div><div><p class="p1"></p></div></div><p><span></span></p></div>

就是这么回事。目前就是想到这个方法了,至于是不是最优就不得而知啦~

 


http://www.ppmy.cn/news/1448294.html

相关文章

网络应用层之(6)L2TP协议详解

网络应用层之(6)L2TP协议 Author: Once Day Date: 2024年5月1日 一位热衷于Linux学习和开发的菜鸟&#xff0c;试图谱写一场冒险之旅&#xff0c;也许终点只是一场白日梦… 漫漫长路&#xff0c;有人对你微笑过嘛… 全系列文档可参考专栏&#xff1a;通信网络技术_Once-Day的…

Django后台项目开发实战七

为后台管理系统换风格 第七阶段 安装皮肤包 pip install django-grappelli 在 setting.py 注册 INSTALLED_APPS [grappelli,django.contrib.admin,django.contrib.auth,django.contrib.contenttypes,django.contrib.sessions,django.contrib.messages,django.contrib.stat…

redis 高可用 Sentinel 详解

写在前面 redis 在我们日常的业务开发中是十分常见的&#xff0c;而redis的可用性就必须要有很高的要求&#xff0c;那么 redis集群的高可用由有一个或者多个 Sentinel(哨兵) 实例组成的 哨兵系统来保证的。 哨兵 由一个或者多个 Sentinel 实例组成的 Sentinel 系统可以监控任…

微软如何打造数字零售力航母系列科普06 - 如何使用微软的Copilot人工智能

如何使用微软的Copilot人工智能&#xff1f; Copilot和ChatGPT有很多相似之处&#xff0c;但微软的聊天机器人本身就有一定的优势。以下是如何对其进行旋转&#xff0c;并查看其最引人注目的功能。 ​​​​​​​ &#xff08;资料来源&#xff1a;Lance Whitney/微软&…

LNMP部署及应用(Linux+Nginx+MySQL+PHP)

LNMP 我们为什么采用LNMP这种架构? 采用Linux、PHP、MySQL的优点我们不必多说。 Nginx是一个小巧而高效的Linux下的Web服务器软件&#xff0c;是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的&#xff0c;已经在一些俄罗斯的大型网站上运行多年&#xff0c;目…

【概念】Linux-用户和权限的概念

前言 本文主要介绍Linux系统中的用户和权限相关概念 用户和用户组 基本概念 概念 Linux系统中关于用户、用户组&#xff0c;可以实现如下操作&#xff1a; 配置多个用户配置多个用户组一个用户可以加入多个用户组里面&#xff1b;一个用户组可以包含多个用户&#xff1b;即…

Spring Web MVC入门(2)——请求

目录 一、传递单个参数 基础类型和包装类型的区别 1、基础类型 &#xff08;1&#xff09;不传参 &#xff08;2&#xff09;传字符串 2、包装类型 &#xff08;1&#xff09;不传参 &#xff08;2&#xff09;传字符串 3、小结 二、传递多个参数 三、传递对象 四、…

前端自定义封装图片预览组件(支持多张图片预览 缩放):

封装图片预览组件&#xff1a; <template><div ref"previewWrapper" class"image-preview"><div class"overlay" v-if"showOverlay" click"closePreview"></div><div class"preview-conta…