JQuery learning note

Source: Internet
Author: User

I started learning jQuery today and found that jQuery is really powerful. There are still a lot of things to learn. First, I will repeat the basic things.

1. Bind a function to be executed when the DOM is ready for query and operation. This method is purely a substitute for the registration time of the window. load event.

$ (Document). ready (function (){

// Write your code here

});

JQuery (function ($ ){

// Write your code here

});

$ (Function (){

// Write your code here

})

 

 

2. Get objects, object values, html, text, attributes, etc.

2.1Get Element

// Match an element based on the given ID.

$ ("# Id ");

// Match all elements based on the given element name

// Element (String): an element used for search. The name of the tag pointing to the DOM node.

$ (Element); $ ("div ");

// Match elements based on the given class.

// Class (String): A Search class. An element can have multiple classes, and can be matched as long as there is a conformity.

$ (Class); $ (". myClass ");

// Combine the elements matched by each selector and return them together.

// You can specify any number of selectors and merge the matched elements into a single result.

// Selector1 (Selector): a valid Selector

// Selector2 (Selector): another valid Selector

// SelectorN (Selector): (optional) any number of valid selectors

$ ("Div, span, p. myClass ")

 

2.2Element html

// Obtain the html content of the First Matching Element.

// This function cannot be used in XML documents.

// But it can be used in XHTML documents.

$ ("# ID" pai.html ();

// Set the HTML content of each matching element.

// This function cannot be used in XML documents.

// But it can be used in XHTML documents.

$ ("# ID" pai.html (VAL );

2.3Text of an element

// Obtain the content of all matching elements.

$ ("# ID"). Text ();

// Set the text content of all matching elements

$ ("# ID"). Text (VAL );

2.4Element value

// Obtain the current value of the First Matching Element

$ ("# ID"). Val ();

// Set the value of each Matching Element

// Check, select, radio, etc.

$ ("# ID"). Val (VAL );

2.5Element attributes

// Obtain the attribute value of the First Matching Element.

// This method can be used to conveniently obtain the attribute value from the First Matching Element.

// If the element does not have the corresponding attribute, undefined is returned.

$ ("# Id"). attr ("id ");

// Set an object in the form of "name/value" to the attributes of all matching elements.

$ ("# Id"). attr ({src: "test.jpg", alt: "Test Image "});

// Set an attribute value for all matching elements.

// Key (String): attribute name

// Value (Object): Attribute value

$ ("# Id"). attr (key, value );

// Set a calculated attribute value for all matching elements.

// A function is provided instead of a value. The value calculated by this function is used as the attribute value.

// Key (String): attribute name

// Fu (Function): Return Value Function range: Current element, parameter; index of current element

$ ("# Id") (key, fn );

// Delete an attribute from the matched element

// Name (String): name of the attribute to be deleted

$ ("# Id"). removeAttr (name );

// Add the specified class name for the Matching Element

// Class (String): one or more CSS class names separated by Spaces

$ ("# Id"). addClass (class );

// Delete the specified class name for the Matching Element

// Class (String): one or more CSS class names separated by Spaces

$ ("# Id"). removeClass (class );

// Delete (ADD) a class if it exists (does not exist.

// Class (String): one or more CSS class names separated by Spaces

$ ("# Id"). toggleClass (class );

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.