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.