从HTML代码创建JavaScript对象

时间:2016-10-21 13:45:12

标签: javascript object dom

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属性链接起来?

2 个答案:

答案 0 :(得分:0)

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

&#13;
&#13;
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));
&#13;
<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>
&#13;
&#13;
&#13;

编辑:从jQuery转换为原生javascript。

答案 1 :(得分:0)

&#13;
&#13;
var div = document.getElementById("html");
var parent = loopul(document.getElementById("html").getElementsByTagName("ul")[0])[0]
function loopul(ul){
var lis = ul.getElementsByTagName("li");
if(lis.length>0){
var arr = [];
for(var i=0;i<lis.length;i++){
	//console.log(lis[i]);
var nm = lis[i].innerText;
if(lis[i].getElementsByTagName("ul").length>0){
nm = nm.replace(lis[i].getElementsByTagName("ul")[0].innerText,"");
}
nm = nm.replace(/[\n\r\t]/g,"").trim();
var du = lis[i].getElementsByTagName("ul").length ? loopul(lis[i].getElementsByTagName("ul")[0]) : [];
arr.push({
"dir":lis[i].id ? lis[i].id : "",
"name":nm,
"type":lis[i].type,
"children":du
});
}
return arr;
}else return [];
}

console.log(parent);
&#13;
<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>
&#13;
&#13;
&#13;