Basic syntax of jQuery, with explanation


In the previous lesson, readers read along with Taimienphi.vn to learn how to create websites with jQuery, in the next lesson below, Taimienphi.vn will introduce you to the most basic jQuery syntax.


Refer to the following article of Taimienphi.vn to learn about the basic syntax of jQuery. Also readers can refer to some other jQuery lessons already on Taimienphi.vn to learn about what jQuery is.

Basic syntax of jQuery

1. Basic syntax of jQuery

JQuery statements usually begin with a symbol ($) and end with semicolon (;)In jQuery, the symbol ($) indicates the jQuery alias.

Let’s consider the following example code which demonstrates the most basic statement of the jQuery.

For example: The following example illustrates the most basic statement in jQuery:

jquery's banquet

The results will display a warning to the user Hello Taimienphi.vn!

2. Explain the code in the above example

In the example above:

– Element script: Because jQuery is just a JavaScript library, jQuery code can be placed inside an element script. However, if you want to put jQuery code in an external JavaScript file, you just need to delete this part.

– $ (document) .ready (handler); This command is interpreted as a ready event. Inside handler is the function passed to the method ready() to enforce security as soon as the document is ready to be processed, ie when the DOM hierarchy has been fully built.

Method ready () In jQuery, it is often used with anonymous functions. So in the example above we can write in abbreviated form as below:

jquery's ban cu 2

Tips: We can use any syntax because both syntaxes are equivalent. However, using the ready event when reading the code will be easier.

Also in the event handler, we can write more jQuery statements to execute any action, like $ (selector) .action ();

Inside $ (selector) Select HTML elements from the DOM tree to manipulate and action () apply some actions on selected elements, such as changing the CSS property value or setting the content of an element, etc.

For example: The following example illustrates how to set the text after the DOM is ready:

jquery's ban cu 3

The output will look like this:

jquery 4 of your ban

In the jQuery statement in the example above (line 10), p is the jQuery selector used to select all text, that is, elements p in the document, then modal text () Set the content of the text to “Xin Chao Taimienphi.vn”.

The text in the above example is automatically replaced when the document is ready. Refer to the example below to see what happens if we ask the user to perform some specific actions before executing jQuery code to replace the text:

Jquery's legal ban 5

The output will look like this:

jquery's ban cu 6

In the example above the text is only replaced when the user clicks the “Replace van ban” button.

Note: We should put jQuery code inside the document ready event so that the code is executed when the document is ready to work.

The above lesson Taimienphi.vn has just introduced you to the basic syntax of jQuery. In the next lesson, Taimienphi.vn will introduce you further Selector in jQuery.

https://thuthuat.taimienphi.vn/cu-phap-co-ban-cua-jquery-51217n.aspx
If you have any questions or questions, you can leave your comments in the comments section below.

.

Add a Comment

Your email address will not be published. Required fields are marked *