Latest web development tutorials

jQuery syntax

By jQuery, you can select (query, query) HTML elements, and they perform the "operation" (actions).


jQuery syntax

jQuery syntax is to perform certain actions by selecting HTML elements, and selected elements.

Basicgrammar:. $ (Selector) action ( )

  • Dollar symbol definition jQuery
  • Selector (selector) "Query" and "Find" HTML element
  • jQuery's action () to perform operations on elements

Example:

  • $ (This) .hide () - Hide elements

  • $ ( "P") hide () -. Hide all <p> elements

  • $ ( "P.test") hide () -. Hide all class = "test" the <p> element

  • $ ( "# Test") hide () -. Hide all id = "test" element

lampCSS selectors you familiar with it?

JQuery syntax using a combination of CSS selectors and XPath syntax. In the next section of this tutorial, you'll learn more about the selector syntax.


Document ready event

You may have noticed that in our case all the jQuery functions in a document ready function:

$ (Document) .ready (function () {

// Start writing jQuery code ...

});

This is to prevent the document is fully loaded operation (ready) before jQuery code.

If the function is run before the document is not fully loaded, the operation might fail. Here are two specific examples:

  • Trying to hide an element that does not exist
  • To obtain an image size is not fully loaded

Tip: simple writing (written with the same effect as above):

$ (Function () {

// Start writing jQuery code ...

});

These two ways you can choose your favorite way to achieve the implementation of jQuery method after the document is ready.