JQuery learning notes 7 methods for building functions

Source: Internet
Author: User

1: $ (selectorStr [, restricted range]), accepts a selector (string conforming to jQuery specifications), and returns a jQuery object;

Copy codeThe Code is as follows:
// $ (Selector [, restricted range])
$ (". Guo"). click (function () {// There is no context parameter here
$ ("A. aguo", this).css ({"color": "red"}); // this is the context parameter, used to limit the range
});

Ii. $ (htmlStr [, Document Object]), $ (html [, json object]) is used to input an html string and create a new dom element.

When the input is a string, the constructor will determine whether it is a selector string or an html string .,
If a selector string is used, the dom is traversed to find the matched elements. If no matching element exists,
An empty jQuery object will be returned; otherwise, a jQuery object will be created for these matched elements.

Copy codeThe Code is as follows:
// $ (HtmlStr [, Document Object])
$ ("<Div> Hello everyone </div>"). append ("body"); // simple tag: no nodes

3. $ (dom element), $ (dom Element Set) converts dom elements to jQuery objects.

The jQuery constructor uses the original js method document. createElement () to create a dom node.

Copy codeThe Code is as follows:
$ ("<Div> <a> Hello everyone </a> </div>"). append ("body"); // complex Tag: Contains subnodes.

4. $ (custom object) encapsulates common objects as jQuery objects.

JQuery will use the document fragment buildFragment () method to insert all the child nodes to [, document Object] at a time. If this parameter is not specified, it will be document; $ (html [, json object])

When html is a simple tag, her second parameter can be a json object that contains the attributes or events of the dom element.

Copy codeThe Code is as follows:
// $ (Html [, json object])
$ ("<Div> </div> ",{
"Class": "gys", // because class is a js keyword, quotation marks are required.
Text: "Hello everyone ",
Click: function () {alert ("What Do I Do ");}
}). Append ("body ");

V. $ (callback function) bind the ready event listening function and execute it when the Dom load is complete.

$ (Element) or $ (elements)

Input a dom object or a set of dom objects

Copy codeThe Code is as follows:
("Div. guo"). click (function (){
$ (This). slideUp ();
});

Vi. $ (jQuery object) accepts a jQuery object and returns a copy of The jQuery object

$ (Object );

Input a Common Object

Copy codeThe Code is as follows:
Var obj = {name: "guo", age: 24 };
Var $ obj = $ (obj );
$ Obj. on ("guo", function (){
Alert ("starting a Custom Event ");
});
$ Obj. trigger ("guo ");

7: $ () creates an empty jQuery object.

$ (Callback) Incoming a function

Copy codeThe Code is as follows:
$ (Function (){})

This indicates that the function is executed after the Dom is loaded.

$ (JQuery Object );

When a jQuery object is input, a copy of the jQuery object is created and returned. The copy is identical to the referenced Dom object of the passed 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.