jQuery syntax
With jQuery, you can select (query) HTML elements and perform "actions" on them.
jQuery syntax
jQuery syntax is to select HTML elements and perform certain operations on the selected elements.
Basic syntax: $(selector).action()
- Dollar sign definition jQuery
- Selector (selector) "query" and "find" HTML elements
- jQuery's action() performs operations on elements
Example:
$(this).hide() - Hide the current element
$("p").hide() - Hide all <p> elements
$("p.test").hide() - Hide all <p> elements with class="test"
$ ("#test").hide() - Hide all elements with id="test"
##Are you familiar with CSS selectors? The syntax used by jQuery is a combination of XPath and CSS selector syntax. In the following chapters of this tutorial, you will learn more about selector syntax. |
Document ready eventYou may have noticed that all the jQuery functions in our example are located in a document ready function :
$(document).ready(function(){
// Start writing jQuery code...
}) ;
This is to prevent jQuery code from running before the document is fully loaded (ready). If you run the function before the document is fully loaded, the operation may fail. Here are two specific examples: // Start writing jQuery code...
}) ;
- Trying to hide a non-existent elementGetting the size of an image that was not fully loaded
Tips: Concise writing (same effect as the above):
$(function(){
// Start writing jQuery code...
});
You can choose the method you like to execute the jQuery method after the document is ready. // Start writing jQuery code...
});