jquery diary 5.31 The creation of jquery objects

Source: Internet
Author: User

jquery object Generation 1 selector null for any convertible false returns empty jquery Object 2 selector for string 2.1 selector as HTML string or tag with id attribute 2.2.1 Selector as HTML character Converts an HTML character to a DOM element and puts it into an array of the current jquery when the context parameter is a JS object, the Iterator property (1) is called when the current property corresponds to a function of this jquery object.            This function, which is the value of the current property. (2) Other Settings HTML Property 2.2.2 Selector has an ID when directly getElementById 2.2 selector for selector expression fix context in context up-up with Find Method 3 SE Lector Wrap this DOM as a DOM element for jquery Object 4 selector as function This function as ready time listener 5 selector as function and does not satisfy the first four branches of this SELECOTR into the current jquery object array
init = JQuery.fn.init = function (selector, context) {var match, elem;//HANDLE: $ (""), $ (null), $ (undefined), $ (false)// Returns an empty jquery object if (!selector) {return this;} Handle HTML Stringsif (typeof selector = = = "string") {//If selector ends with ' > ' at the beginning of ' < ' and is longer than 3 that it is an HTML string, does not proceed rquickexp R match if (selector[0] = = = < && selector[Selector.length-1] = = = ">" && selector.length >= 3) {//Assume that strings-start and end with <> is HTML and skip the regex checkmatch = [NULL, selector, NULL] ;} else {match = rquickexpr.exec (selector);} Match HTML or make sure no context are specified for #idif (match && (match[1] | |!context)) {//HANDLE: $ (HTM L)-$ (array) if (Match[1]) {///If the context is a jquery object, take the first DOM element context = Context instanceof JQuery? Context[0]: Contex t;//scripts is true for back-compat//intentionally let the error being thrown if parsehtml is not present//the HTML converted DOM element is combined And when the current JQuery object Jquery.merge (This, jquery.parsehtml (Match[1], Context && Context.nodetype? context.ownerdocument | | context:document,true));//HANDLE: $ (HTML, props)//Handle the first parameter as an HTML string The second parameter is a JS object if (Rsingletag.test (match[1]) &&amp ; Jquery.isplainobject (context)) {//For-each context element for (match in context) {//Properties of the context is called as Methods if possible//if the current JQuery object Match property is a function if (Jquery.isfunction (this[match])) {//execute match function this[match] (context [Match]); /... and otherwise set as attributes//otherwise, set HTML property} else {this.attr (match, context[match]);}} Return this;//HANDLE: $ (#id)} else {elem = document.getElementById (match[2]);//Check parentnode to catch when Blackber Ry 4.6 returns//nodes that is no longer in the document #6963//Blackberry 4.6 cache over, node not in document still found to if (Elem & & Elem.parentnode) {//Inject the element directly into the jQuery objectthis.length = 1;this[0] = elem;} Set the context object to Documentthis.context = Document;this.selector = Selector;return this;} HANDLE: $ (expr, $ (...)) Handle no context parameter or the context parameter is a jquery object} else if (!context | | context.jquery) {//If there is no context parameter then call the Find method in the document scope Find//IF There is a context parameter to find the return (context | | rootjquery) In this context. Find (selector);//HANDLE: $ (expr, context)//(which is just EQ Uivalent to: $ (context). Find (expr)//handle selector as expr, the second parameter is the context selector case} else {//Select the context again Findreturn This.constructor (context). Find (selector);}  HANDLE: $ (domelement)//wrap DOM element as jquery object} else if (selector.nodetype) {this.context = this[0] = selector;this.length = 1;return this;//HANDLE: $ (function)//Shortcut for document ready//If selector is function//bind function as ready listener, or execute immediately (r Ootjquery.ready = = = "undefined")} else if (Jquery.isfunction (selector)) {return typeof Rootjquery.ready!== "undefined "? Rootjquery.ready (Selector)://Execute immediately if Ready was not presentselector (jQuery);} If Selctor is a function or an object, and there is a selector element when the if (selector.selector!== undefined) {this.selector = Selector.selector;this.context = SelectoR.context;} Put selector into the current JQuery object's array return Jquery.makearray (selector, this);};


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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.