jquery Learning Notes

Source: Internet
Author: User

The following code is executed after the page frame has been downloaded, similar to Window.onload=function () {}, but more efficient than the latter, which must wait until everything on the page has finished loading before executing the code.

$ (document). Ready (function() {   //          code executed })
Equivalent to
$ (function () {

})

Used to write directly JS native, such as:

var t1=document.getelementbyid ("MM")// get id=mm dom object var t2=t1.innerhtml; // get the contents of the DOM object t2.innerhtml= "Set the contents of the DOM object";

In jquery, getting DOM objects is a lot easier to use, as follows: (class with Dot. ID with # selection)

var t1=$ (#mm); // gets the DOM object for the id=mm var t2=t1.html (); // gets the contents of the object t2.html ("Set content in the object");

So, using jquery is a lot simpler than traditional JS code, and it's also compatible with a variety of browsers.

jquery selector:

1. Basic Selector

// ID match element, select Id=divone DOM node $ ("#divOne"). CSS ("Display", "block");//Display Id=divone page elements // the element name matches the element $ ("div span"). CSS ("Display", "block");//display element named span page element // class matching element, select class= Clsone DOM node $ (". Clsone"). CSS ("Display", "block");//page element with category name Clsone // match all elements $ ("*"). CSS ("Display", "block"// Merge matching elements $ ("#divOne, span"). CSS ("Display", "block"); /display id=divone and element named span page elements

2. Hierarchy selector: The main hierarchical relationship includes descendants, father and son, neighbor, sibling relationship

//match descendant elements$("Div span"). CSS ("Display", "block")//Display all SAPN tags in div//Match child elements$("Div>span"). CSS ("Display", "block");//display div neutron span marker//Match subsequent elements$ ("#divMid + div"). CSS ("Display", "block")///display the next div$ after the ID is divmid element ("#divMid"). Next (). CSS ("Display", "block"); //match all subsequent elements$ ("#divMid ~ div"). CSS ("Display", "block");//display all div$ with ID divmid element ("#divMid"). Nextall (). CSS ("Display", "block"); //match all adjacent elements$ ("#divMid"). Siblings ("div"). CSS ("Display", "block");//display all adjacent div with ID divmid

3. Simple filter selector, the writing is preceded by a colon

//Add a category for the first element$ ("Li:first"). AddClass ("GetFocus"));//Add the first Li class class//Add a category for the last element$ ("Li:last"). AddClass ("GetFocus"));//Add the last Li class class//increased removal of all element categories matching the given selector$ ("Li:not" (. Notclass) addclass ("GetFocus").); //Increase all element categories with an even number of index values, counting from 0$ ("Li:even"). AddClass ("GetFocus")); //Add all element categories with an odd index value, counting from 0$ ("li:odd"). AddClass ("GetFocus")); //adds an element class for a given index value, counting from 0$ ("Li:eq (1)"). AddClass ("GetFocus"); //Increase all element categories greater than the given index value, counting from 0$ ("LI:GT (1)"). AddClass ("GetFocus"); //Increase all element categories less than the given index value, counting from 0$ ("Li:lt (4)"). AddClass ("GetFocus"); //add title class element category$ ("div H1"). CSS ("width", "238");$(": Header"). AddClass ("GetFocus")); Animateit (); //Add animation effect element category$ ("#spnMove: Animated"). AddClass ("GetFocus"); functionAnimateit () {//animation effects$ ("#spnMove"). Slidetoggle ("slow"), Animateit); }

4. Content Filter Selector: Gets an element based on the content of the text in the element or the contained child elements, whose text content can be positioned in a fuzzy or absolute match for the element

// displays the element containing the given text $ ("Div:contains (' A ')"). CSS ("Display", "block")           ; // displays all empty elements that do not contain child elements or text $ ("Div:empty"). CSS ("Display", "block")            ; // Displays the element $ ("Div:has (span)") that contains the element that the selector matches . CSS ("Display", "block"); // displays the element containing the child element or text $ ("div:parent"). CSS ("Display", "block")         ;

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.