-
-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DOM2JSON #36
Comments
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="box">
<p class="p">hello world</p>
<div class="person">
<span class="name">DOM2JSON</span>
<span class="age">100</span>
</div>
</div>
<script>
function DOM2JSON(domTree){
if(!domTree) return;
let rootObj = {
tagName: domTree.tagName,
children: []
}
const children = domTree.children
if(children){
Array.from(children).forEach((element, i) => {
rootObj.children[i] = DOM2JSON(element)
})
}
return rootObj
}
console.log(DOM2JSON(document.querySelector('.box')))
</script>
</body>
</html> |
function DOMtoJSON(node) {
const obj = {
tag: node.tagName.toLowerCase(),
children: [],
};
for (let i = 0; i < node.children.length; i++) {
obj.children.push(DOMtoJSON(node.children[i]));
}
return obj;
} |
function dom2json(domtree) {
let obj = {};
obj.tag = domtree.tagName.toLowerCase();
obj.children = [];
domtree.children.forEach((item) => {
obj.children.push(dom2json(item));
});
return obj;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: