Java technology sharing: A brief analysis of jquery

Source: Internet
Author: User

# A brief analysis of jquery

# # A jquery overview

jquery is a JS library, which encapsulates a lot of JS method, equivalent to an external JS file.

# # # 1. Quick Start

1) Guide Library

2) use jquery directly in Scrpit.

"' HTML
<script type= "Text/javascript" src= ". /js/jquery-1.8.3.js "></script>
<script>
$(
function () {
$ ("#span1"). html ("Hello jquery!");
}
);
</script>
```

# # # 2.jquery page load function (functions executed when the page is finished loading)

Window.onload can only appear in one, and the following will overwrite the previous, but jquery's multiple page load functions do not overwrite

jquery page Load function

"' JavaScript
Way One
JQuery (document). Ready (function () {//...});
Way two:
$ (). Ready (function () {//...});
Way three: Commonly used
$ (function () {
Content
});
```

# # # 3.jquery Conversions between objects and Dom objects

-dom objects are converted to jquery objects, so you can use the properties and methods in the JQuery object

"' JavaScript
$ (function () {
var SP1 = document.getElementById ("SP1");
sp1.innerhtml = "hello!";
$ (SP1). html ("Hello jquery!");

});
```

-The jquery object is converted to a DOM object so that you can use the properties and methods in the DOM object

"' JavaScript
$ (function () {
$ ("#sp1"). Get (0). InnerHTML = "hello!";
});
```

# # II, jquery selector

By looking at the jquery documentation, there are a variety of jquery selectors, commonly used selectors as follows:

# # 1. Basic Selector

-ID Selector:

"' JavaScript
$ ("#btn1");
```

-Element Selector

"' JavaScript
$ ("div");
```

-Class Selector

"' JavaScript
$ (". Mini");
```

-All elements

"' JavaScript
$("*")
```

-Merging multiple selectors

"' Javas
$ ("Div,.mini");
```

# 2. Hierarchy Selector

-$ ("Grandfather Descendants"): Descendants including sons and grandchildren
-$ ("Grandpa > Son"): only son, no grandson
-$ ("Brother + Brother"): Brother must be next to brother, other distant brother not counted
-$ ("Brother ~ brother"): Brother can not be next to each other, only peers.

# 3. Basic Filter Selector

-First (): Select a
-Last (): Select final
-even: Select an element with an even number of indexes
-odd: Select an element with an odd index

# 4. Property Selector

-[Properties]
-[attribute = ' value ']

# # Three, the case of the provinces and cities two-level linkage

To achieve level two linkage, you must understand the use of each function in jquery.

Syntax format for each function:

$.each (DataSet, callback function (I,n)), where I refers to the loop variable for Each loop, and n refers to the element in the data set that corresponds to each loop.

The code for the two-level linkage of provinces and municipalities is as follows:

```
<! DOCTYPE html>


<meta charset= "UTF-8" >
<title></title>
<script type= "Text/javascript" src= ". /js/jquery-1.8.3.js "></script>
<script>
$ (function () {
var cities = new Array ();
Cities[0] = new Array ("Wuhan", "Huanggang", "Xiangyang City", "Jinzhou");
CITIES[1] = new Array ("Changsha", "Chenzhou", "Zhuzhou", "Yueyang");
CITIES[2] = new Array ("Shijiazhuang City", "Hanhan", "Langfang", "Baoding");
CITIES[3] = new Array ("Zhengzhou", "Luoyang", "Kaifeng", "Anyang");
$ ("#province"). Change (function () {
$ ("#city"). empty ();
1. Obtain the value selected by the current select
var val = this.value;
2. Iterate through the array to find the corresponding city of the province
$.each (Cities,function (i,n) {
if (val==i) {
Find the appropriate province
$.each (Cities[i],function (j,m) {
var tn = document.createTextNode (Cities[i][j]);
var op = document.createelement ("option");
$ (TN). AppendTo ($ (OP));
$ (OP). AppendTo ($ ("#city"));
});
}
})
});
});
</script>

<body>
<select id= "Province" >
<option>--Please select--</option>
<option value= "0" > Hubei </option>
<option value= "1" > Hunan </option>
<option value= "2" > Hebei </option>
<option value= "3" > Henan </option>
</select>
<select id= "City" >
</select>
</body>

```

Java technology sharing: A brief analysis of jquery

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.