说废话没用呢
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <script> var vnode = { tag: 'ul', props: { class: 'list' }, text: '', children: [ { tag: "li", props: { class: "item" }, text: '', children: [ { tag: undefined, props: {}, text: '牛客网', children: [] } ] }, { tag: "li", props: {}, text: '', children: [ { tag: undefined, props: {}, text: 'nowcoder', children: [] } ] } ] } const _createElm = vnode => { // 补全代码 let element = undefined if (vnode.tag) { element = document.createElement(vnode.tag) const props = Object.keys(vnode.props) props.forEach(prop => { element.setAttribute(prop, vnode.props[prop]) }) if (vnode.children.length > 0) { vnode.children.forEach(subVnode => { const subElem = _createElm(subVnode) if (typeof subElem === 'string') { element.innerText = subElem } else { element.appendChild(subElem) } }) } } else { element = vnode.text } return element } </script> </body> </html>