日期:2014-05-16 浏览次数:20342 次
<div id="box">mybox</div> (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(fn){ for(var i=0, len = this.elements.length; i<len; i++){ fn.call(this,this.elements[i]); console.log(this.elements[i]) } }, setStyle : function(prop,val){ this.each(function(el){ console.log(el) el.style[prop] = val; }) } } window.$ = function(){ return new _$(arguments); } })(); $("box").setStyle("color","red")