jquery Tutorial: Simplifying jquery

Source: Internet
Author: User
Tags array extend key return string split window

This is the previous collation of some code, and Jqer share, I hope that some help.

Say the more powerful jquery volume is no longer small, 55k (Minified) size, although not very large, but if used in a number of small projects or sites, it is not small, then we do not according to their own project characteristics, simplifying or rewriting jquery, Follow me!

The first is the core code of jquery:

Code:

Not exactly the same, but the means of implementation are generally the same
(function () {
window._$ = window.$;
var $ = window.$ = function (s) {
return new $.fn.init (s);
};
$.fn = $.prototype = {
Init:function (s) {
if (!s) return this;
if (S.nodetype) {
THIS.E = [];
This.e.push (s); Storage-Acquired elements
return this;
}
if (typeof s = = "string") {
return $ (). find (s);
}
return this;
},
Find:function (s) {
Look up the DOM based on the incoming string
}
};
Extend extension method
$.fn.extend = function (p) {
for (var key in P) {
if (!$.fn[key]) {
$.fn[key] = P[key];
}
}
};
$.fn.init.prototype = $.fn;
})();
$ (). Extend ({
' A ': function (s) {},
"B": function (s) {}
});
and attach my simplified version.

Code:

by Cnwander
(function () {
window._$ = window.$;
var eleexpr =/([#\.a-za-z]) ([^\s]+)/g;
var $ = window.$ = function (s) {
return new $.fn.init (s);
};
$.fn = $.prototype = {
Init:function (s) {
THIS.E = null;
if (!s) return this;
if (S.nodetype) {
THIS.E = [];
This.e.push (s);
return this;
}
if (typeof s = = "string") {
return $ (). find (s);
}
Else
return this;
},
Find:function (s) {
var wrap = THIS.E [document];
var result = [];
for (var key in wrap) {
var target = Wrap[key];
while (Eleexpr.test (s)) {
var-i = regexp.$1,
Content = regexp.$2;
target = $ (). Clean (Target,content,first);
}
if (target = = null)
result = NULL;
Else
for (var i = 0; i < target.length; i++)
Result.push (Target[i]);
}
THIS.E = result;
return this;
},
Clean:function (Wrap,content,type) {
if (!wrap) return null;
Wrap = Wrap instanceof Array? Wrap: [Wrap];
var result = new Array ();
for (var key in wrap) {
var temp;
Switch (type) {
Case "#":
temp = Wrap[key].getelementbyid (content);
Break
Case ".":
Temp = $ (). Getelemsbyclassname (Content,wrap[key]);
Break
Default
temp = Wrap[key].getelementsbytagname (type+content);
}
if (temp) {
temp = temp.length? Temp: [temp];
for (var i = 0; i < temp.length; i++)
if (temp[i].nodetype) Result.push (Temp[i]);
}
}
result = Result.length <= 0? Null:result;
return result;
},
Getelemsbyclassname:function (Classname,elem,tag) {
Tag = Tag "*";
Elem = Elem document;
Elem = Elem instanceof Array? Elem: [Elem];
var result = new Array ();
for (var key in Elem) {
var allelems = elem[key].getelementsbytagname (tag) elem[key].all;
var Oelem;
for (var i=0; iOelem = Allelems[i];
var list = OElem.className.split ("");
for (Var j=0 jif (list[j] = = classname) Result.push (Oelem);
}
}
}
Return result.length <= 0? Null:result;
}
};
$.fn.extend = function (p) {
for (var key in P) {
if (!$.fn[key]) {
$.fn[key] = P[key];
}
}
};
$.fn.init.prototype = $.fn;
})();
$.ajax = function (Url,poststr,lastfunc,errfunc) {
var Ajax = false;
if (window. XMLHttpRequest) {
Ajax = new XMLHttpRequest ();
if (Ajax.overridemimetype) {
Ajax.overridemimetype ("Text/xml");
}
}
else if (window. ActiveXObject) {
try {
Ajax = new ActiveXObject ("Msxml2.xmlhttp");
}
catch (e) {
try {
Ajax = new ActiveXObject ("Microsoft.XMLHTTP");
}
catch (e) {
}
}
}
if (!ajax) {
if (Errfunc) Errfunc ();
return false;
}
Ajax.open ("POST", url, True);
Ajax.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Ajax.send (POSTSTR);
Ajax.onreadystatechange = function () {
if (ajax.readystate = = 4 && ajax.status = 200) {
if (Lastfunc) Lastfunc (Ajax.responsetext);
}
}
}
$ (). Extend ({
' HTML ': function (val) {
return val = = undefined?
(This.e[0]?
This.e[0].innerhtml:
NULL):
this.e[0].innerhtml = val;
},
' Empty ': function () {
for (var i in THIS.E) {
var o = this.e[i];
o.innerhtml = ";
}
return this;
},
"CSS": function () {
if (!THIS.E arguments.length <= 0) return to this;
if (arguments.length = = 1 && typeof arguments[0] = = "string") {
Arguments[0] = Arguments[0].tolowercase ();
Return This.e[0].currentstyle?
This.e[0].currentstyle[arguments[0]]:
window.getComputedStyle (This.e[0], ""). GetPropertyValue (Arguments[0].replace ([[A-z])/g, "-$1″");
}
else if (arguments.length >= 2) {
for (var i in THIS.E)
This.e[i].style[arguments[0]] = arguments[1];
}
},
' Hasclass ': function (name) {
if (!THIS.E) return this;
var allclass = This.e[0].classname.split ("");
for (var key in Allclass)
if (allclass[key] = = name) return true;
return false;
},
' AddClass ': function (name) {
if (THIS.E)
for (var i in THIS.E)
if (This.e[i].nodetype = 1)
This.e[i].classname + = "" +name;
return this;
},
' Removeclass ': function (name) {
if (THIS.E)
for (var i in THIS.E) {
var temp = [],
Allclass = This.e[i].classname.split ("");
for (var j = 0, k = 0; J < Allclass.length; J + +) {
if (Allclass[j]!= name) {
temp[k++] = Allclass[j]
}
}
Allclass = Temp.join ("");
This.e[i].classname = Allclass;
}
return this;
},
' MouseDown ': function (callback) {
if (!THIS.E) return this;
for (var key in THIS.E) {
This.e[key].onmousedown = callback;
return this;
}
},
' MouseOver ': function (callback) {if (!THIS.E) return This;for (Var key in this.e) {this.e[key].onmouseover = callback} Return this},
' Mouseout ': function (callback) {if (!THIS.E) return This;for (Var key in this.e) {this.e[key].onmouseout = Callback}return This
});
Talents, the wrong place, please correct me.

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.