var vDom = createElement('div',{class:'container'},[
createElement('p',{class:'item',style:'color:red'},'我是节点1'),
createElement('p',{class:'item'},'我是节点2'),
createElement('p',{class:'item'},'我是节点3'),
createElement('input',{class:'item'},'我是节点3'),
]);
function Element(type,props,children){
this.type = type;
this.props = props;
this.children = children;
}
function createElement(type,props,children){
return new Element(type,props,children)
}
function render(obj){
let el = document.createElement(obj.type)
for(let [key,value] of Object.entries(obj.props)){
el.setAttribute(key,value)
};
if(Array.isArray(obj.children)){
obj.children.forEach(item=>{
item = render(item)
el.appendChild(item)
})
}else{
el.appendChild(document.createTextNode(obj.children))
}
return el
}
function renderDom(el,target){
target.appendChild(el)
}
renderDom(render(vDom),document.body)