案例--添加class

let obj = {
tagName: "div",
props: {
style: "font-size:12px",
class: "wrap"
},
children: {
tagName: "p",
children: "hello rs6"
}
};

function createElement(obj) {
let { tagName, props, children } = obj;
let el = document.createElement(tagName);

document.body.appendChild(el);
if (props) {
for (let i in props) {
el.setAttribute(i, props[i]);
}
}
if (typeof children === "object") {
el.appendChild(createElement(children))
} else {
el.innerHTML = children;
}
return el;
}
createElement(obj);

你可能感兴趣的:(案例)