<! 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 classes
return new JQuery.fn.init (selector, context);
Returns an instance of a selector
}
The prototype object of the jquery class
Jquery.fn = Jquery.prototype = {
Init:function (Selector, context) {
Defining the selector constructor
Selector = Selector | | Document
Set default value to document
Context = Context | | Document
Set default value to document
if (Selector.nodetype) {
If the selector is a node object
This[0] = selector;
To pass a parameter node to an array of instance objects
This.length = 1;
and set the length property of the instance object to define the number of elements contained
This.context = selector;
Set the properties of the instance to return to the selection range
return this;
Returns the current instance
}
if (typeof selector = = = = "string") {
If the selector is a string
var e = context.getelementsbytagname (selector);
Gets the element with the specified name
for (var i = 0; i < e.length; i++) {//iterates through the elements collection and fills all the elements into the current instance array
This[i] = E[i];
}
This.length = E.length; Sets the length property of the instance, which defines the number of elements contained
This.context = context; Set the properties of the instance to return to the selection range
return this; Returns the current instance
} else {
this.length = 0; Otherwise, set the value of the length property of the instance to 0
This.context = context;
return this; Returns the current instance
}
},
jquery: "1.3.2",
Prototype properties
Size:function () {
Prototyping methods
return this.length;
}
}
JQuery.fn.init.prototype = Jquery.fn;
Use jquery's prototype object to overwrite Init's prototype object
Alert ($ ("div"). Size ());
Returns 3
</script>
Emulate jquery framework source code-Mature---Selector