This function is used to create a new jQuery object, then a set of DOM elements jQuery added to the stack, and returns the jQuery object, there are three parameters, as follows:
elems Array type will be pressed into the jQuery stack array elements, for generating a new jQuery object
name Optional. jQuery method name of type String generation of array elements
selector optional. Array type Query parameters passed to the method (for serialization)
2 and 3 parameters optional parameters, selector jQuery object properties of the new setting for the return
JQuery object before calling within the DOM after pushStack references will not disappear, but also save to a new object prevObject, we can end () jQuery objects prior to acquisition, such as:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="http://libs.baidu.com/jquery/1.7.1/jquery.min.js"></script> </head> <body> <p id="p1">今天天气很好</p><Really>= "P2"the above mentioned idthe p-</p> <button id="b1">get News</button><button id="b2">get olds</button> <script> var a = $('p1'); b1.onclick=function(){ a=a.pushStack([p2],'Test','arg'); console.log(a); } b2.onclick=function(){ a=a.prevObject; console.log(a) } console.log(a); //初始化时a.selector="p1" </script> </body> </html>
Render as follows:
Output initialization follows:
p1 is the preservation of a DOM node, then click on get News, output is as follows:
At this time, a storage node p2 in the DOM, and then click get olds, output:
Back to the state of the initialization
Source code analysis
writer by: Desert QQ: 22969969
pushStack jQuery.fn defined on the inside, as follows:
= = jQuery.prototype the jQuery.fn { / ** / pushStack: function (the elems, name, Selector) { // Create a new empty jQuery object, then the elements of the set into the jQuery DOM object, and retain the current reference to the jQuery object, which provides support for a number of ways. // the Build jQuery Matched Element A new new SET var RET = the this .constructor (); // Construct a new, empty jQuery object RET IF (jQuery.isArray (elems)) { // If elems parameter is an array, the array method borrowed push () insert push.apply (RET, the elems); } the else { jQuery.merge (RET, the elems); // else calls method jQuery.merge (first, second) combined. } // the Add Object The Old Onto The Stack (AS A Reference) ret.prevObject = the this ; // set properties on a new Query object prevObject RET, points to the current jQuery object, thereby forming a stack chain ret.context = the this . context; IF (name === "Find" ) { ret.selector = the this .selector + ( the this .selector "?": "") + Selector; } the else IF (name) { ret.selector = the this .selector + "." + name + "(" + Selector + ")"; //Save the name attribute selector and generating selector jQuery object to the years, such as: .appendTo (P) } // the Return Formed Element The newly-SET return ret; // finally returns the new jQuery object ret } / ** / };
It is created internally to create a new jQuery object and return link is established through prevObject property and jQuery object before it.