var T= (function(){css
return new Base();html
});數組
function Base(){this
//建立一個數組,保存得到的節點htm
this.elements = [];element
this.id = function(param){get
this.elements.push(document.getElementById(param));io
return this;function
};cli
this.tag = function(name){
var tags = document.getElementsByTagName(name);
for(var i=0;i<tags.length;i++){
this.elements.push(tags[i]);
}
return this;
};
this.css = function(attr,value){
for(var i=0;i<this.elements.length;i++){
this.elements[i].style[attr] = value;
}
return this;
};
this.html = function(str){
for(var i=0;i<this.elements.length;i++){
this.elements[i].innerHTML = str;
}
return this;
};
this.click = function(fn){
for(var i=0;i<this.elements.length;i++){
this.elements[i].onclick = fn;
}
return this;
}
};
使用例子:
window.onload = function(){
T().id("t").css("color","red");
T().tag("p").css("color","green");
}