- A $ sign to define/access JQuery
- A(selector) to "query (or find)" HTML elements
- A jQuery action() to be performed on the elements(s)
examples:
$(this).hide() - hides the current element.
$("p').hide() - hides all <p> elements
$(".test").hide() hides all elements with class ="test"
$("#test").hid() hides all elements with id = "test"
Are you familiar with CSS selector?
jQuery uses CSS syntax to select elements. You will learn more about he selector syntax in the next chapter of this tutorial.
The Document Ready Event
$(document).ready(function ()
{
}
This is to prevent any jQuery code from running before the document is finished loading (is ready).
More Examples of jQuery Selectors
Syntax | Description | Example |
---|---|---|
$("*") | Selects all elements | |
$(this) | Selects the current HTML element | |
$("p.intro") | Selects all <p> elements with class="intro" | |
$("p:first") | Selects the first <p> element | |
$("ul li:first") | Selects the first <li> element of the first <ul> | |
$("ul li:first-child") | Selects the first <li> element of every <ul> | |
$("[href]") | Selects all elements with an href attribute | |
$("a[target='_blank']") | Selects all <a> elements with a target attribute value equal to "_blank" | |
$("a[target!='_blank']") | Selects all <a> elements with a target attribute value NOT equal to "_blank" | |
$(":button") | Selects all <button> elements and <input> elements of type="button" | |
$("tr:even") | Selects all even <tr> elements | |
$("tr:odd") | Selects all odd <tr> elements |
Use our jQuery Selector Tester to demonstrate the different selectors.
functions In a Separate File
If your website contains a lot of pages, and you want your jQuery functions to be easy to maintain, you can put your jQury functions in a separate .js file.
When we demonstrate jQuery in this tutorial, the functions are added directly into teh <head> section. However, sometimes it is preferable to place them in a separate file, like (use the src attribute to refer to the .js file).
No comments:
Post a Comment