从HTML代码创建JavaScript对象

用户名

的HTML

<div id="html">
  <ul>
    <li id="myFolder" type="folder">myFolder
      <ul>
        <li id="myFolder/fonts" class="empty" type="folder">fonts</li>
        <li id="myFolder/index.html" type="file">index.html</li>
        <li id="myFolder/js" type="folder">js
          <ul>
            <li id="myFolder/js/controllers" type="folder">controllers
              <ul>
                <li id="myFolder/js/controllers/core" type="folder">core
                  <ul>
                    <li id="myFolder/tempjs/controllerslates/core/menu.js" type="file">menu.js</li>
                  </ul>
                </li>
                <li id="myFolder/js/controllers/errors" type="folder">errors
                  <ul>
                    <li id="myFolder/js/controllers/errors/error.js" type="file">error.js</li>
                  </ul>
                </li>
              </ul>
            </li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</div>

我想将HTML转换成几个这样的对象:

{
    "dir": "",
    "name": "myFolder",
    "type": "folder",
    "children": [{
        "dir": "myFolder",
        "name": "fonts",
        "type": "folder"
    }, {
        "dir": "myFolder",
        "name": "index.html",
        "type": "file"
    }, {
        "dir": "myFolder",
        "name": "js",
        "type": "folder",
        "children": [{
            "dir": "myFolder/js",
            "name": "controllers",
            "type": "folder",
            "children": [{
                "dir": "myFolder/js/controllers",
                "name": "core",
                "type": "folder",
                "children": [{
                    "dir": "myFolder/tempjs/controllerslates/core",
                    "name": "menu.js",
                    "type": "file"
                }]
            }, {
                "dir": "myFolder/js/controllers",
                "name": "errors",
                "type": "folder",
                "children": [{
                    "dir": "myFolder/js/controllers/errors",
                    "name": "error.js",
                    "type": "file"
                }]
            }]
        }]
    }]
}

我不知道如何进行转换。我想我可以用

document.getElementsByTagName('li');

或类似的东西。但是,如何管理父母和孩子的链接,以及如何链接具有HTML属性的对象的变量?

塔哈厚

这是我能得到的最接近的。享受。

var getIdBasedStructure = function(ulContainer) {
  var output = [];
  Array.prototype.forEach.call(ulContainer.children, function(el) {
    if (el.nodeName == "LI") {
      var ret = getIdSplits(el.id, el.type);
      if (el.querySelectorAll("ul").length > 0) {
        output.push({
          dir: ret[0],
          name: ret[1],
          type: ret[2],
          children: getIdBasedStructure(el.querySelectorAll("ul")[0])
        });
      } else {
        output.push({
          dir: ret[0],
          name: ret[1],
          type: ret[2]
        });
      }
    }
  });
  return output;
};

var getIdSplits = function(id, type) {
  var split = id.split("/");
  var ret = [];
  if (split.length > 3) {
    ret[2] = split[split.length - 1];
    ret[1] = split.slice(0, split.length - 1).join("/");
    ret[0] = type;
  } else {
    ret[0] = split[0] ? split[0] : null;
    ret[1] = split[1] ? split[1] : null;
    ret[2] = type;
  }
  return ret;
}

var structure = getIdBasedStructure(document.querySelectorAll("#html > ul")[0]);
console.log(JSON.stringify(structure));
<div id="html">
  <ul>
    <li id="myFolder" type="folder">myFolder
      <ul>
        <li id="myFolder/fonts" class="empty" type="folder">fonts</li>
        <li id="myFolder/index.html" type="file">index.html</li>
        <li id="myFolder/js" type="folder">js
          <ul>
            <li id="myFolder/js/controllers" type="folder">controllers
              <ul>
                <li id="myFolder/js/controllers/core" type="folder">core
                  <ul>
                    <li id="myFolder/tempjs/controllerslates/core/menu.js" type="file">menu.js</li>
                  </ul>
                </li>
                <li id="myFolder/js/controllers/errors" type="folder">errors
                  <ul>
                    <li id="myFolder/js/controllers/errors/error.js" type="file">error.js</li>
                  </ul>
                </li>
              </ul>
            </li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</div>

编辑:从jQuery转换为本机javascript。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章