Emulate jquery framework source code-Mature---Selector

Source: Internet
Author: User

<! DOCTYPE html>
<meta charset= "UTF-8" ><title></title><style type= "Text/css" >. Red {color:red;}</style><div class= "Red" >1</div><div>2</div><div>3</div></body><script language= "javascript" type= "Text/javascript" >var $ = JQuery = function (selector, context) {Defining classesreturn new JQuery.fn.init (selector, context);Returns an instance of a selector}The prototype object of the jquery classJquery.fn = Jquery.prototype = {Init:function (Selector, context) {Defining the selector constructorSelector = Selector | | DocumentSet default value to documentContext = Context | | DocumentSet default value to documentif (Selector.nodetype) {If the selector is a node objectThis[0] = selector;To pass a parameter node to an array of instance objectsThis.length = 1;and set the length property of the instance object to define the number of elements containedThis.context = selector;Set the properties of the instance to return to the selection rangereturn this;Returns the current instance}if (typeof selector = = = = "string") {If the selector is a stringvar e = context.getelementsbytagname (selector);Gets the element with the specified namefor (var i = 0; i < e.length; i++) {//iterates through the elements collection and fills all the elements into the current instance arrayThis[i] = E[i];}This.length = E.length; Sets the length property of the instance, which defines the number of elements containedThis.context = context; Set the properties of the instance to return to the selection rangereturn this; Returns the current instance} else {this.length = 0; Otherwise, set the value of the length property of the instance to 0This.context = context;return this; Returns the current instance}},jquery: "1.3.2",Prototype propertiesSize:function () {Prototyping methodsreturn this.length;}}JQuery.fn.init.prototype = Jquery.fn;Use jquery's prototype object to overwrite Init's prototype objectAlert ($ ("div"). Size ());Returns 3</script>

Emulate jquery framework source code-Mature---Selector

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.