<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> native JavaScript faux-write jquery</title>
<body>
<div>
<div class= "Wrap" >
<input type= "Text"/>
<input type= "Text"/>
<input type= "Text"/>
</div>
<div class= "Wrap" >
<input type= "Text"/>
<input type= "Text"/>
<input type= "Text"/>
</div>
<div class= "Wrap" >
<input type= "Text"/>
<input type= "Text"/>
<input type= "Text"/>
</div>
</div>
<div>
<div class= "Wrap" >
<input type= "Text"/>
<input type= "Text"/>
<input type= "Text"/>
</div>
<div class= "Wrap" >
<input type= "Text"/>
<input type= "Text"/>
<input type= "Text"/>
</div>
<div class= "Wrap" >
<input type= "Text"/>
<input type= "Text"/>
<input type= "Text"/>
</div>
</div>
<div>
<div class= "Wrap" >
<input type= "Text"/>
<input type= "Text"/>
<input type= "Text"/>
</div>
<div class= "Wrap" >
<input type= "Text"/>
<input type= "Text"/>
<input type= "Text"/>
</div>
<div class= "Wrap" >
<input type= "Text"/>
<input type= "Text"/>
<input type= "Text"/>
</div>
</div>
<script type= "Text/javascript" >
There are three types of parameters:
1. String: Selector
2.function:ready method
3. Native object: Native object to JQ object
//==================
Native object to JQ
function JQ (ARG) {
Array used to place native objects
This.elements = [];
Switch (typeof Arg) {
Case "string":
this.elements = Getele (ARG);
Break
Case "function":
Ready (ARG);
Break
Case "Object":
This.elements.push (ARG);
Break
}
}
JQ object transferred to native, take operation
JQ.prototype.get = function (index) {
Return this.elements[index];//Native JS object
return new JQ (This.elements[index]); JQ Object
}
$ ("div"). EQ (2);
EQ: Removes a native object from the JQ object and then turns it into a new JQ object
JQ.prototype.eq = function (index) {
return new JQ (This.elements[index]);
}
$ ("div"). EQ (2). CSS ()
//==================
$ ("input"). CSS (). CSS ().
function css () {
return this;
// }
//==================
Function Ready (FN) {
domcontentloaded notifications from the browser after DOM loading is complete
if (Window.addeventlistener) {
Window.addeventlistener ("domcontentloaded", FN, false);
} else {
Defer indicates that the script tag is loaded when the DOM is loaded.
Before you IE8, learn
var script = document.createelement ("script");
Script.defer = true;
var head = document.getElementsByTagName ("head") [0];
Head.appendchild (script);
Script.onreadystatechange = function () {
if (script.readystate = = "complete") {
FN ();
}
}
}
return window;
}
//===========================
$ ("xx xx xxx") Multilevel selector case
function Getele (selecter) {
var reg =/^\s+|\s+$/g;
Selecter = Selecter.replace (Reg, "");
arr = [XX,XX,XX];
var arr = selecter.split ("");
var parents = [Document];
var children = [];
Justification $ ("div. Wrap input") For example
First loop: Arr[i] is div,parents is Document,children is the div in the current document
Second cycle: Arr[i] Yes, wrap,parents is all the Div,children is all the Div.wrap
Third cycle: Arr[i] is input,parents is all the Div.wrap,children is all the div.wrap in the input
for (var i = 0; i < arr.length; i++) {
children = [];
for (var j = 0; J < Parents.length; J + +) {
Find class
if (/^\./.test (Arr[i])) {
var nodes = Getelebyclass (Parents[j], ARR[I].SUBSTR (1));
Console.log (nodes);
for (var k = 0; k < nodes.length; k++) {
Children.push (Nodes[k]);
//}
children = nodes;
Find ID
} else if (/^#/.test (Arr[i])) {
var nodes = Parens[j].getelementbyid (ARR[I].SUBSTR (1));
Children.push (nodes);
Find TagName
} else {
var nodes = Parents[j].getelementsbytagname (Arr[i]);
Console.log (nodes);
Console.log (nodes);
for (var k = 0; k < nodes.length; k++) {
Children.push (Nodes[k]);
//}
children = nodes;
}
}
Parents = children;
}
return children;
}
Getele ("div. Wrap input"); //=== === === === === === ===
$ ("div") when the parameters of JQ have only one level selector
function Getele (selecter) {
1. Remove the leading and trailing spaces
var reg =/^\s+|\s+$/g;
Selecter.replace (Reg, "");
2. Judging selector type
if (/^#/.test (Selecter)) {
ID Selector
return document.getElementById (Selecter);
} else if (/^./.test (Selecter)) {
Class Selector
Return Getelebyclass (Selecter);
} else {
Return document.getElementsByTagName (Selecter);
//}
//}
//=== === === === === === ===
Because Getelementsbyclassname has compatibility, it is handled separately
function Getelebyclass (selecter) {
Modern browsers
if (document.getelementsbyclassname) {
Return Document.getelementsbyclassname (Selecter)
} else {
IE does not support direct lookup based on classname, so a traversal is required
var nodes = document.getElementsByTagName ("*");
var arr = [];
for (var i = 0; i < nodes.length; i++) {
if (Nodes[i].classname = = Selecter) {
Arr.push (Nodes[i]);
//}
//}
return arr;
//}
//}
function Getelebyclass (parentobj, Selecter) {
Modern browsers
if (parentobj.getelementsbyclassname) {
Return Parentobj.getelementsbyclassname (Selecter)
Compatible with IE8 version of the browser
} else {
var nodes = parentobj.getelementsbytagname ("*")
var arr = [];
for (var i = 0; i < nodes.length; i++) {
if (Nodes[i].classname = = Selecter) {
Arr.push (Nodes[i])
}
}
Return arr
}
}
//============================
Console.log (typeof this);
Event ****************
Two parameters
1. Event name
2. Callback function
JQ.prototype.on = function (EventName, FN) {
for (var i = 0; i < this.elements.length; i++) {
Addevent (EventName, This.elements[i], FN);
}
}
Because Addeventlistner has compatibility issues, IE8 used Attachevent
function addevent (eventName, obj, fn) {
if (Obj.addeventlistener) {
Obj.addeventlistener (EventName, FN, false)
} else {
IE8 below
Obj.attachevent ("on" + EventName, fn)
}
}
JQ.prototype.click = function () {
for (var i = 0; i < this.elements.length; i++) {
Addevent ("Click", This.elements[i], FN);
//}
//}
css***************
JQ.prototype.css = function () {
There are two types of parameters when using CSS
1. There are two cases of one parameter
1) string $ ("div"). CSS ("color");
2) Object $ ("div"). css ({color: "Red"}), which means assigning a value to a style
2. Two parameters for direct to a style assignment $ ("div"). CSS ("Color", "red")
Switch (arguments.length) {
Case 1:
$ ("div"). css ({
Color: "Red",
Width: "100",
//})
if ((typeof arguments[0]) = = = "Object") {
Assign a value to a style
For (prop in Arguments[0]) {
for (var i = 0; i < this.elements.length; i++) {
SetStyle (This.elements[i], prop, Arguments[0][prop]);
}
}
} else {
To take a value from a style
/* may have more than one native object in the JQ object, take only the first style */
Return GetStyle (This.elements[0], arguments[0]);
}
Break
Case 2:
for (var i = 0; i < this.elements.length; i++) {
For example: $ ("div"). CSS ("Color", "red")
SetStyle (This.elements[i], arguments[0], arguments[1]);
}
Break
}
}
Get style
/*
1. Object to get the style
2. Style name */
function GetStyle (obj, stylename) {
if (window.getComputedStyle) {
return window.getComputedStyle (obj, null) [StyleName];
} else {
Ie8
Return Obj.currentstyle (StyleName);
}
}
Set style
/*
1. The object to assign a value to
2. Style name
3. The value of the style */
function SetStyle (obj, StyleName, stylevalue) {
var arr = ["Left", "right", "width", "height"];
for (var i = 0; i < arr.length; i++) {
If the style you want to set is within the range of the array, you need to take "px"
if (arr[i] = = stylename) {
If "px" is not in the value, add "px"
if (!/px/.test (Stylevalue)) {
Stylevalue = Stylevalue + "px";
}
}
}
Obj.style[stylename] = Stylevalue;
}
function $ (ARG) {
return new JQ (ARG);
}
Console.log (Getele ("div. Wrap input"));
Test
Console.log ($ ("div. Wrap input"));
$ ("div"). On ("click", Function () {
Alert ("Not about");
Event.cancelbubble = true;
//
//})
$ (". Wrap"). CSS ("BackgroundColor", "Red");
$ ("div"). css ({
BackgroundColor: "Yellow",
//})
$ (function () {
Alert ("Hello World");
Console.log ("Hello javaScript");
$ ("input"). EQ (0). CSS ("BackgroundColor", "Red");
$ ("input"). CSS ("BackgroundColor", "Red");
$ ("input"). Get (5). Style.backgroundcolor = "Blue";
$ ("input"). Get (9). CSS ("BackgroundColor", "yellow");
});
</script>
</body>
Native JavaScript-like examples of jquery