我需要通过一个类来操作HTML标签,如下所示:
每次调用
谢谢。
/*
* Begin: HTML Class
*/
HTML = function(el, property) { // Construct
this.el = el;
this.property = new Array;
var HTML = document.createElement(el);
this.element = function() {
return HTML;
};
HTML.objects.push(this);
if (typeof property == "object")
for (i in property)
this.addProperty(i, property[i]);
};
HTML.objects = new Array; // Registers all new HTML objects.
// Adds a new property to HTML current element.
HTML.prototype.addProperty = function(name, value) {
this.property[name] = value;
this.getHTML()[name] = value;
};
// Retrieves current HTML element.
HTML.prototype.getHTML = function() {
return this.element();
};
// Clones current HTML objects with same construct arguments.
HTML.prototype.clone = function() {
return new HTML(this.el, this.property);
};
/*
* End: HTML Class
*/
每次调用
new HTML(...)
时,新创建的实例必须存储在HTML.objects
中,它是HTML的静态属性,其作用是跟踪所有HTML对象。但现在当它到达HTML.objects.push(this);
时,它将返回undefined属性错误。之后,我尝试在Firebug中调用HTML.objects
,它肯定是已定义的。由于在实例化时调用了function(...) { ... }
,所以它应该能够访问HTML.objects
属性吗?谢谢。
HTML
和HTML
之间的名称冲突。 - Bergi