Introduction to JQuery
jquery
Literally means javascript
and 查询
, a js
library used to aid development . jquery
Greatly simplifies js
programming. <script>
Use the src
attribute to introduce the jquery.js
address in the label , and then you can use the jquery
syntax normally . Generally use the 1.x version, you can see which version the giant company uses.
The console outputs "hello world"
$(document).ready(function (){
console.log("hello world")
});
Hidden id=test
element
$("#test").hide()
jquery function
- HTML element selection
- HTML element manipulation
- CSS operations
- HTML event function
- JavaScript special effects and animation
- HTML DOM traversal and modification
- AJAX
JQuery selector
grammar | meaning |
---|---|
$("*") |
Select all elements |
$(this) |
Select the current HTML element |
$("p.intro") |
Select the p tag with class intro |
$("p.first") |
First p tag |
$("ul li:first") |
The first li tag element of the ul tag |
$("[href]") |
Elements with href attribute |
$("a[target='x']") |
a tag with target value x |
$(":button") |
input tag and button tag with type="button" |
jquery选择器
Allows operations on groups of HTML elements or individual elements.
jquery选择器
It can be based on元素id
,类
,类型
,属性
,属性值
and other select HTML elements can also be based on existingcss选择器
; in addition, it also has some custom selectors!
JQuery events
jquery is specially designed for event handling! ! !
The response of the page to different visitors is called an event, and the event handler refers to the method that is called when certain events occur in the HTML.
Mouse event | Keyboard events | Form event | Document/window events |
---|---|---|---|
click | keypress | submit | load |
dbclick | keydown | change | resize |
mouseenter | keyup | focus | scroll |
mouseleave | blur | unload |
JQuery DOM manipulation
A very important part of jquery is DOM manipulation capabilities. The following three simple and practical jquery methods for DOM manipulation:
text()
: Set or get the text content of the selected elementhtml()
: Same as above (will contain HTML markup)val()
: Set or return the value of the form fieldattr()
: Get the value of the specified attribute
The above four methods can set the value of the selected element, form value, attribute value
Introduction to AJAX
ajax = async javascript and xml
(Asynchronous ja and xml); ajax
not a new programming language, but a new method using existing standards; it ajax
is a technology that can update part of the web page without reloading the entire page; it ajax
is based on XHR
technology and server Exchange data!
XHR
Request step
- Initialize the XHR object
- Set the request method, URL, and whether the XHR object is asynchronous
- send request
- Monitor state changes of XHR objects
- Processing return result
var xhrhtml = new XMLHttpRequest();
xhrhtml.open("get", "https://www.qq.com", true);
xhrhtml.send();
xhrhtml.onreadystatechange = function () {
if(xhrhtml.readState == 4 && xhrhtml.status == 200){
document.write(xhrhtml.responseText);
console.log("suceess")
}
else {
console.log("Faild")
}
}
XHR attributes
Attributes | meaning |
---|---|
status | Return status code |
responseText | Data returned as text |
responseXML | Data returned in XMl form |
statusText | Status returned as a string |
readyState | The status code of the xhr request: 0~4 0: The request is not initialized 1: The connection is established with the server 2: The request has been received 3: The request is being processed 4: The request has been completed |
onreadystatechange | readyState Execute the function when a change occurs |
XHR method
method | meaning |
---|---|
open() | Set the request method, URL, whether it is asynchronous |
send() | send request |
setRequestHeader() | Request header |
Can
JSON.parse()
beresponseText
converted tojson
format
About AJAX and JQuery
jquery
Provides multipleajax
related methodsjquery
Solved theajax
compatibility issue between different browsersjquery
Encapsulatedjquery
typeajax
method
$.ajax({
type: "GET",
url: "htpps://www.qq.com",
dataType: "jsonp",
data: {
},
success: function(data, status){
if(status == 200){
console.log("success")
}
console.log("data is "data)
}
});
Ajax method encapsulated in JQuery
method | meaning |
---|---|
$.ajax() |
Perform ajax request, asynchronous |
$.get() |
Execute get request |
$.getJSON() |
Execute get request and return json format data |
$.post() |
Execute post request |
$.param() |
创建对象的序列化形式 |
ajaxsend() |
ajax请求发送之前执行 |
ajaxstart() |
第一个ajax请求开始时执行 |
ajaxstop() |
所有ajax请求完成时执行 |
ajaxsuccess() |
ajax请求成功时执行 |
ajaxError() |
ajax请求失败时执行 |
ajaxComplete() |
ajax请求完成时执行 |
ajaxload() |
从服务器加载数据,并把返回数据放到指定元素中 |
Template模版
artTemplate
,腾讯开源,效率很高!
模版使用:
- 引入模版
js文件
- 定义模版:
<script>
标签中定义 - 数据+模版生成HTML:
template() 方法
- 将生成的HTML渲染到页面
template()
方法:
- 将数据和模版结合起来,返回HTML
- 通过
模版ID
来引用模版
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>天气查询</title>
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/1.11.3/jquery.js"></script>
<script type="text/javascript" src="http://cdn.staticfile.org/artTemplate.js/3.0.1/template.js"></script>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<style type="text/css">
#container1{
height: 400px;
width: 300px;
background-color: #409EF0;
text-align: center;
margin: auto;
padding: 10px;
}
#container2{
font-size: 30px;
color: #FFFFFF;
}
#container3{
color: #FFFFFF;
}
ul{
list-style: none;
text-align: left;
}
#query,#city{
border-radius: 8px;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$("#query").click(function(){
var city = $("#city").val();
$.ajax({
type: "GET",
url: "http://api.tianapi.com/txapi/tianqi/index",
data: {
key: "xxxxxxxxxxxxxxxx",
city: city
},
success: function(data, status){
console.log(city, data);
var html = template("resultTemp", data);
var container = document.querySelector("#container3");
container.innerHTML = html;
}
});
});
});
</script>
<script type="text/html" id="resultTemp">
<ul>
<li><b>城市</b>:{
{
newslist[0]["area"]}}</li>
<li><b>日期</b>:{
{
newslist[0]["date"]}}</li>
<li><b>天气</b>:{
{
newslist[0]["weather"]}}</li>
<li><b>当前气温</b>:{
{
newslist[0]["real"]}}</li>
<li><b>最低气温</b>:{
{
newslist[0]["lowest"]}}</li>
<li><b>最高气温</b>:{
{
newslist[0]["highest"]}}</li>
<br>
<li><b>生活提示</b>:{
{
newslist[0]["tips"]}}</li>
</ul>
</script>
</head>
<body>
<div id="container1">
<div id="container2">
全国天气查询
</div>
<hr>
<input type="text" name="city" id="city" placeholder="请输入城市名称">
<button id="query">查询</button>
<div id="container3">
<ul>
<li><b>城市</b>:</li>
<li><b>日期</b>:</li>
<li><b>天气</b>:</li>
<li><b>当前气温</b>:</li>
<li><b>最低气温</b>:</li>
<li><b>最高气温</b>:</li>
<br>
<li><b>生活提示</b>:</li>
</ul>
</div>
</div>
</body>
</html>