JQuery basic syntax Summary

Source: Internet
Author: User

JQuery basic syntax Summary

In daily development, JQuery is the most frequently used JS library. To use JQuery for development, you must first understand the basic syntax of JQuery. Below are some simple syntaxes summarized when learning JQuery.

 

 

1. $ (document) converts a document Object to jquery

 

The Code is as follows:


$ (Document). ready (function (){
Alert ("hello world ");
});

 

2. Get all hyperlink objects and add an onclick event. In fact, the array of each tag obtained from the underlying jquery object does not need to be cyclically

 

The Code is as follows:


$ (Document). ready (function (){
$ ("A"). click (function (){
Alert ("hello world ");
});
});

 

3. Conversion between jquery objects and dom objects

 

The Code is as follows:


$ (Document). ready (function (){
Var javascriptElement = document. getElementById ("clickme ");
// Convert a dom object to a jquery object
Var jqueryElement = $ (javascriptElement );
// Alert ("javascript:" + javascriptElement. innerHTML); // the content of the innerHTML tag.
// Alert ("jquery:" + jqueryElement.html ());
// Convert jquery to dom object method 1
Var jElement = $ ("# clickme ");
Var javascriptEle = jElement [0];
Alert ("jquery1:" + javascriptEle. innerHTML );
// Method 2
Var javascriptEle2 = jElement. get (0 );
Alert ("jquery2:" + javascriptEle2.innerHTML );
});

 

4. jquery solves the problem of id value

 

The Code is as follows:


<A id = "hello"> click me </a>
<Script type = "text/javascript">
// Solve the problem of id null in the traditional way.
If (document. getElementById ("helllo ")){
// An error occurs if hello does not exist.
Document. getElementById ("helllo"). style. color = "red ";
}
// Method 2
// $ ("# Hello") [0]. style. color = "red ";
// Solve the problem with jquery
$ ("# Hello" ).css ("color", "red ");
// If there is only one parameter, this method is read. If there are two parameters, there are some functions (css (""), css ("", "").
Alert ($ ("# hello" ).css ("color "));
</Script>

 

5. It is stipulated in javascript that you should remove '-' from the attribute when operating the css attribute and uppercase the last letter. For example, you must change background-color to backgroundColor.

6. jquery also uses selectors to manipulate various elements. It inherits the css design concept, but carries it forward;

7. Several writing forms of jquery

1> method 1

 

The Code is as follows:


$ ("Document"). ready (function (){
...
});

 

2> method 2

 

The Code is as follows:


$ (). Ready (function (){
...
});

 

3> method 3

 

The Code is as follows:


$ (Function (){
...
});

 

The above methods are the same

I hope this article will help you learn jquery programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.