Git Product home page Git Product logo

Comments (3)

mengqiuleo avatar mengqiuleo commented on August 15, 2024
<!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>

from js-challenges.

kangkang123269 avatar kangkang123269 commented on August 15, 2024
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;
}

from js-challenges.

bearki99 avatar bearki99 commented on August 15, 2024
function dom2json(domtree) {
  let obj = {};
  obj.tag = domtree.tagName.toLowerCase();
  obj.children = [];
  domtree.children.forEach((item) => {
    obj.children.push(dom2json(item));
  });
  return obj;
}

from js-challenges.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.