<div class="codetitle"> <span><a style="CURSOR: pointer" data="70679" class="copybut" id="copybut70679" onclick="doCopy('code70679')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code70679"> <br> <br> <br> <br><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <br><title>Each Document</title> <br><script type="text/javascript"> <BR>(function(){ //å建ä¸ä¸ªèªæ§è¡å½æ° <BR>function _$(el){ //声æä¸ä¸ªç±» <BR>this.elements = []; //ç¨äºä¿åDOMå ç´ çå¼ç¨ <BR>for (var i = 0; i < el.length; i++) { <BR>var element = el[i]; <BR>if (typeof element == string ) { <BR>element = document.getElementById(element); <BR>} <BR>this.elements.push(element); <BR>} <BR>} <br><br>_$.prototype = { <BR>each: function(fn){ <BR>for (var i = 0; i < this.elements.length; i++) { <BR>fn.call(this, this.elements[i]); //第äºä¸ªåæ°æ¯fnå½æ°çåæ° <BR>} <BR>}, <BR>setStyle: function(prop, val){ <BR>var that = this; <BR>this.each(function(el){ //注æåæ°ç设置 <BR>el.style[prop] = val; <BR>}); <BR>return this; <BR>} <BR>} <br><br>window.$ = function(){ <BR>return new _$(arguments); <BR>}; <BR>})(); <br><br>window.onload = function(){ <BR>$("p", "p2").setStyle("color", "red"); <BR>} <BR></script> <br> <br> <br><p id="p"> <br>Hello world <br></p> <br><p id="p2"> <br>Welcome. <br></p> <br> <br> <br><br> </div>