With JQuery, you can select (query) HTML elements and perform actions on them.
JQuery Syntax Examples
$ (this). Hide ()
Demonstrates the JQuery hide () function, which hides the current HTML element.
$ ("#test"). Hide ()
Demonstrates the JQuery hide () function, which hides elements of the id= "test".
$ ("P"). Hide ()
Demonstrates the JQuery hide () function, hiding all <p> elements.
$ (". Test"). Hide ()
Demonstrates the JQuery hide () function, hiding all elements of the class= "test".
JQuery syntax
The jQuery syntax is for the selection of HTML elements, and you can perform certain operations on elements.
The underlying syntax is:$ (selector). Action ()
· Dollar sign definition JQuery
· Selector (selector) "Query" and "find" HTML elements
· JQuery Action () performs an operation on an element
Example
$ (this). Hide ()-hides the current element
$ ("P"). Hide ()-Hides all paragraphs
$ (". Test"). Hide ()-hides all elements of class= "test"
$ ("#test"). Hide ()-hides all elements of the id= "test"
tip:The syntax used by JQuery is a combination of XPath and CSS selector syntax. In the next chapters of this tutorial, you will learn more about selectors syntax.
Document-Ready functions
You may have noticed that all jQuery functions in our instance are in a document ready function:
$ (document). Ready (function () {
---jQuery functions go here----
});
This is to prevent the document from running JQuery code before it is fully loaded (ready).
If you run the function before the document is fully loaded, the operation may fail. Here are two specific examples:
· Trying to hide an element that doesn't exist
· Get the size of an image that is not fully loaded
JQuery Selector
Selectors allow you to manipulate element groups or individual elements.
JQuery Selector
In the previous chapters, we showed some examples of how to select HTML elements.
The key point is to learn how the JQuery selector accurately selects the elements that you want to apply the effect to.
The JQuery element selector and the property selector allow you to select HTML elements by tag name, property name, or content.
Selectors allow you to manipulate HTML element groups or individual elements.
In the HTML DOM terminology:
Selectors allow you to manipulate a group of DOM elements or a single DOM node.
JQuery element Selector
JQuery uses CSS selectors to select HTML elements.
$ ("P") select the <p> element.
$ ("P.intro") selects all the <p> elements of the class= "Intro".
$ ("P#demo") selects all <p> elements of the id= "demo".
JQuery Property Selector
JQuery uses an XPath expression to select an element with a given property.
$ ("[href]") selects all elements with an href attribute.
$ ("[href= ' # ']") selects all elements with an HREF value equal to "#".
$ ("[href!= ' # ']") selects all elements with an HREF value that is not equal to "#".
$ ("[href$= '. jpg ']") selects all elements with an href value ending with ". jpg".
JQuery CSS Selector
The JQuery CSS selector can be used to change the CSS properties of HTML elements.
The following example changes the background color of all p elements to red:
Instance
$ ("P"). CSS ("Background-color", "Red");
Try it yourself.
More Selector instances
Grammar |
Describe |
$ (This) |
Current HTML Element |
$ ("P") |
All <p> elements |
$ ("P.intro") |
All <p> elements of class= "Intro" |
$ (". Intro") |
All elements of class= "Intro" |
$ ("#intro") |
id= Elements of "Intro" |
$ ("ul Li:first") |
The first <li> element of each <ul> |
$ ("[href$= '. jpg ']") |
All href attributes with attribute values ending with ". jpg" |
$ ("Div#intro. Head") |
id= all class= "head" elements in the <div> element of "Intro" |
Web Development Technology--jquery2 (syntax and selectors)