Two. jquery Source code parsing construction jquery's construction function 7 ways to use jquery

Source: Internet
Author: User
Tags event listener

A: $ (selectorstr[, limit Range]), accepts a selector (a string that conforms to the jquery specification), and returns a JQuery object;
Two: $ (htmlstr[, Document Object]), $ (Html[,json object]) passing in an HTML string, creating a new DOM element
Three: $ (DOM Element), $ (collection of DOM elements) converts DOM elements into jquery objects.
Four: $ (custom object) encapsulates an ordinary object as a jquery object.
Five: $ (callback function) binds the Ready event listener function to execute when DOM loading is complete.
Six: $ (jquery object) accepts a jquery object and returns a copy of a jquery object
Seven: $ () creates an empty jquery object.

1 // $ (selector[, limit Range]) 2         $ (". Guo"). Click (function () {/// There is no context parameter 3this             ). css ({" Color ":" Red "}); // This is the context parameter for scoping 4         });

When a string is passed in, the constructor determines whether it is a selector string or an HTML string.
If it is a selector string: Traverse the DOM to find the element that matches it, and if there are no matching elements,
Returns an empty jquery object, otherwise the matching elements are created to a jquery object.

1 // $ (htmlstr[, Document Object]) 2         // Simple label: No tape node

The jquery constructor creates a DOM node using JS's original ecological Method Document.createelement ()

// Complex Tags: contains child nodes.

jquery uses the document fragment Buildfragment () method to insert all child nodes into the [, document Object] all at once.
If it is not specified here, it is the document;

$ (Html[,json object])

When HTML is a simple tag, then her second argument can be a JSON object that contains the properties or events of the DOM element.

1 // $ (Html[,json object]) 2  $ ("<div></div>", {3             /// because class is a keyword for JS, so quote 4             text: "Everyone good",5             function () {alert ("What do I Do");} 6         }). Append ("Body");

$ (Element) or $ (elements)

Pass in a DOM object or a set of Dom objects

1 ("Div.guo"). Click (function  () {2             $ (this). Slideup (); 3         });

$ (object);

Pass in an ordinary object

        var obj = {name: "Guo", age:24 };         var $obj = $ (obj);        $obj. On (function  () {            alert ("Set up a custom event");        });        $obj. Trigger ("Guo");

$ (callback) to pass in a function

$ (function () {})

This represents the execution of the function after the DOM has been loaded.

$ (jQuery Object);

When a jquery object is passed in, a copy of the jquery object is created and returned, and the copy is referenced exactly to the same DOM object as the incoming jquery object.

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.