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") ;