我正在创建自定义HTML元素,并且已经读过你不应该扩展HTMLElement类,而是为它创建一个包装器。我希望能够使用与标准元素相同的语法来操作属性,属性和方法:点表示法:myDiv.id = "id"
和括号表示法:myDiv["id"] = "id"
。我也希望与方法兼容。全部使用香草js。
以下是包装div
的概念尝试以及我能够弄清楚的。
id
,innerHTML
等on
语法的事件 - 这似乎适用于getter和setter。setAttribute
) - 这些似乎需要手动定义每个元素document.body.appendChild(myDiv)
我的问题按重要性顺序排列:
document.body.appendChild(myDiv)
等操作中使用?
window.onload = function() {
var myDiv = new DivWrapper();
document.body.appendChild(myDiv.div);
// using the style property object
myDiv.style.height = "200px";
myDiv.style.width = "200px";
myDiv.style.background = "aliceblue";
// using manually created methods setAttribute() getAttribute()
myDiv.setAttribute("id", "newId");
// using automatically generated getter and setter
myDiv.innerHTML = "Height: " + myDiv.style.height;
myDiv.innerHTML += "<br>id: " + myDiv.getAttribute("id");
myDiv.contentEditable = "true";
// listener - using getter setter
myDiv.onclick = function(e) {
console.log("Click")
};
}
DivWrapper = (function() {
var instanceCounter = 0;
function DivWrapper() {
this.id = "div" + instanceCounter++;
this.div = document.createElement("div");
this.style = this.div.style;
var attributes = ["align", "class", "contentEditable", "id", "innerHTML", "lang", "tabindex", "title", "value"];
this.createGetSet(attributes);
var methods = ["onclick", "ondblclick", "onmousedown", "onmouseup", "onmouseover", "onmousemove", "onmouseout", "onkeypress", "onkeydown", "onkeyup"];
this.createGetSet(methods);
}
DivWrapper.prototype.createGetSet = function(attributes) {
// for (var i = 0; i < attributes.length; i++) {
var _this = this;
attributes.forEach(function(attribute) {
var getter = function() {
return _this.div[attribute];
};
var setter = function(val) {
_this.div[attribute] = val;
};
Object.defineProperty(_this, attribute, {
get: getter,
set: setter
})
})
}
DivWrapper.prototype.setAttribute = function(attribute, value) {
this.div[attribute] = value;
}
DivWrapper.prototype.getAttribute = function(attribute) {
value = null;
try {
value = this.div[attribute];
} catch (e) {}
return value;
}
return DivWrapper;
})();