Created
November 7, 2018 03:04
-
-
Save aimuz/34641663a4817ae5d3b2148bee807794 to your computer and use it in GitHub Desktop.
虚拟dom 生成实体dom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var domNode = { | |
tagName: "ul", | |
props: { class: "list" }, | |
children: [{ | |
tagName: "li", | |
children: ["1"], | |
}, { | |
tagName: "li", | |
children: ["2"], | |
}, { | |
tagName: "li", | |
children: ["2", { | |
tagName: "div", | |
children: ["div"], | |
}], | |
} | |
] | |
} | |
a = function (b) { | |
var el; | |
if (typeof (b) != "object") { | |
return el | |
} | |
if (b.tagName == "") { | |
return | |
} | |
el = document.createElement(b.tagName) | |
if (b.props && typeof (b.props) == "object") { | |
for (let n in b.props) { | |
el.setAttribute(n, b.props[n]) | |
} | |
} | |
if (typeof (b.children) == "object") { | |
b.children.map((v) => { | |
if (typeof (v) == "string") { | |
el.textContent = v | |
} else { | |
let cc = a(v) | |
el.append(cc) | |
} | |
}) | |
} | |
return el | |
} | |
console.log(a(domNode)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment