Imitating the chain call of the jQuery each function

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.