JQuery 2.1.4 version of source code analysis
Source code Analysis of acquisition elements in jquery
Jquery.each ({
Gets the parent element of the current element:function(elem) {varParent =Elem.parentnode;
The node type of NodeType 11 is documentfragmentreturnParent && Parent.nodetype!== 11? Parent:NULL; },
Get all parent nodes This involves the Dir method parents:function(elem) {returnJquery.dir (Elem, "ParentNode"); }, Parentsuntil:function(Elem, I, until) {returnJquery.dir (Elem, "ParentNode", until); }, Next:function(elem) {returnSibling (Elem, "nextSibling"); }, Prev:function(elem) {returnSibling (Elem, "previoussibling"); }, Nextall:function(elem) {returnJquery.dir (Elem, "nextSibling"); }, Prevall:function(elem) {returnJquery.dir (Elem, "previoussibling"); }, Nextuntil:function(Elem, I, until) {returnJquery.dir (Elem, "nextSibling", until); }, Prevuntil:function(Elem, I, until) {returnJquery.dir (Elem, "previoussibling", until); },
Return all sibling nodes siblings:function(elem) {returnJquery.sibling ((Elem.parentnode | |{}). FirstChild, Elem); }, Children:function(elem) {returnjquery.sibling (Elem.firstchild); },
Returns all the contents of a frame or all the child nodes of an element contents:function(elem) {returnelem.contentdocument | |Jquery.merge ([], elem.childnodes); } }, function(name, FN) {Jquery.fn[name]=function(until, selector) {varmatched = Jquery.map ( This, FN, until); if(Name.slice ( -5)!== "Until") {Selector=until; } if(Selector &&typeofselector = = = "string") {Matched=Jquery.filter (selector, matched); } if( This. length > 1) { //Remove Duplicates if(!Guaranteedunique[name]) {Jquery.unique (matched); } //Reverse order for parents* and Prev-derivatives if(rparentsprev.test (name)) {matched.reverse (); } } return This. Pushstack (matched); }; });
Jquery.extend ({
Look for the parent until you find the end of until. Dir = parentnode dir According to the parents method:function(Elem, dir, until) {varmatched =[], truncate= until!==undefined; The condition to enter the loop is not the root node and the parent node is copied to Elem while((Elem = Elem[dir]) && Elem.nodetype!== 9) { if(Elem.nodetype = = 1) {
Jump out of the loopif(Truncate &&jQuery (Elem). is (until)) { Break; } matched.push (Elem); } } returnmatched; },//through the above source next method to know Elem = nextsiblilng sibling:function(n, elem) {varmatched = []; for(; n; n =n.nextsibling) {if(N.nodetype = = = 1 && n!==elem) {Matched.push (n); } } returnmatched; } });
Let's talk about the difference between nextSibling and nextelementsibling.
NextSibling: Sibling node After returning element (contains comment text)
Nextelementsibling: Sibling node After returning element does not include comment and text and his property is read only
Jquery.noconflict Source Code Analysis
var //To mount jquery under a Windows object_jquery =Window.jquery,//mount the $ to Windows object_$ =window.$; When calling Jquery.noconfict deep ==false then the $ alias fails if Deep==true then Jquery's alias is invalidated Jquery.noconflict=function(deep) {if(window.$ = = =jQuery) {window.$= _$; } if(Deep && window.jquery = = =jQuery) {Window.jquery=_jquery; } returnJQuery; }; //Expose jQuery and $ identifiers, even in AMD //(#7102 #comment:10, https://github.com/jquery/jquery/pull/557) //and CommonJS for browser emulators (#13566) if(typeofNoglobal = = =strundefined) {Window.jquery= window.$ = JQuery;
2016-11-22 21:14:38
JQuery 2.1.4 version of source code analysis