/* 方法的调用链 */
(function(){
function _$(els){
this.elements = [];
for(var i=0,len = els.length ;i<len; ++i){
var element = els[i];
if (typeof element === 'string') {
element = document.getElementById(element);
}
this.elements.push(element);
}
}
_$.prototype = {
each: function(){
for( var i=0, len=this.elements.length;i<len; ++i;){
fn.call(this, this.elements[i]);
}
return this;
},
setStyle: function(prop, val){
this.each(function(el){
el.style[prop] = val;
});
return this;
},
show: function(){
var that = this;
this.each(function(el){
that.setStyle('display','block');
});
return this;
},
addEvent: function(){
var add = function(el){
if (window.addEventListtener) {
el.addEventListtener(type, fn, false);
}
else if (window.attachEvent) {
el.attachEvent('on'+type, fn);
}
};
this.each(function(el){
add(el);
});
return this;
}
};
window.$ = function(){
return new _$(arguments);
};
})();
$(window).addEvent('load',function(){
$('test-1','test-2').show().setStyle('color','red').addEvent('click',function(e){
$(this).setStyle('color','green');
});
});
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛