JS
class Element {
//追加的节点名称,节点属性,节点样式,追加的位置
constructor(tag, attr, style, father) {
//添加节点
this.createTag(tag);
//添加属性
this.setAttr(attr);
//添加样式
this.setStyle(style)
//追加到父节点
this.append(father)
}
//添加节点
createTag(tag) {
this.newTag = document.createElement(tag);
console.log(this.newTag);
}
//添加属性
setAttr(attr) {
for (let n in attr) {
this.newTag[n] = attr[n];
}
}
//添加样式
setStyle(style) {
for (let i in style) {
this.newTag.style[i] = style[i];
}
}
//追加到父节点
append(father) {
father.appendChild(this.newTag);
}
}
new Element("img",
{ src: "../images/1.jpg" },
{ opacity: 0.6, borderRadius: "50%",height:"500px" },
document.body
)