通过构造函数封装简单库
function Base(){
this.elements = [];
this.getId = function(id){
this.elements.push(document.getElementById(id));
return this;
};
this.getTagName = function(tag){
var tagName = document.getElementsByTagName(tag);
for(var i = 0; i < tagName.length;i++){
this.elements.push(tagName[i]);
};
return this;
}
}
在其原型上面添加处理方式,
Base.prototype.css = function(attr,value){
for(var i = 0;i< this.elements.length;i++){
this.elements[i].style[attr] = value;
};
return this;
}
Base.prototype.click = function(fn){
for(var i = 0;i<this.elements.length;i++){
this.elements[i].onclick = fn;
}
return this;
}
经过测试没有问题
$().getId("mydiv").css('color','red').click(function(){
alert(1)
});
欢迎各位提出问题