Copy codeThe Code is as follows: <! Doctype html public "-// W3C // dtd html 4.01 // EN" "http://www.w3.org/TR/html4/strict.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = iso-8859-1">
<Title> Each Document </title>
<Script type = "text/javascript">
(Function () {// create an self-executed function
Function _ $ (el) {// declare a class
This. elements = []; // used to save references to DOM elements
For (var I = 0; I <el. length; I ++ ){
Var element = el [I];
If (typeof element = string ){
Element = document. getElementById (element );
}
This. elements. push (element );
}
}
_ $. Prototype = {
Each: function (fn ){
For (var I = 0; I <this. elements. length; I ++ ){
Fn. call (this, this. elements [I]); // The second parameter is the fn function parameter.
}
},
SetStyle: function (prop, val ){
Var that = this;
This. each (function (el) {// note parameter settings
El. style [prop] = val;
});
Return this;
}
}
Window. $ = function (){
Return new _ $ (arguments );
};
})();
Window. onload = function (){
$ ("P", "p2"). setStyle ("color", "red ");
}
</Script>
</Head>
<Body>
<P id = "p">
Hello world
</P>
<P id = "p2">
Welcome.
</P>
</Body>
</Html>