Skip to content

Instantly share code, notes, and snippets.

@aimuz
Created November 7, 2018 03:04
Show Gist options
  • Save aimuz/34641663a4817ae5d3b2148bee807794 to your computer and use it in GitHub Desktop.
Save aimuz/34641663a4817ae5d3b2148bee807794 to your computer and use it in GitHub Desktop.
虚拟dom 生成实体dom
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