1. jQuery
1.0 导入
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
1.1 页面加载成功函数
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script>
$(function(){
alert('弹出框')
});
$(document).ready(function(){
alert('弹出框2.。。。。')
});
jQuery(function(){
alert('33333')
})
</script>
</head>
<body>
</body>
</html>
1.2 选择器
$("#值").函数...
$("标签名").函数...
$(".值").函数...
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script>
$(function(){
$("#my1").height("100px");
$("div").width("100px");
$(".myclass").css("background-color","green");
});
</script>
</head>
<body>
<div id="my1" class="myclass"></div>
</body>
</html>
1.3 事件绑定
$("#id").click( 函数 )
$("#id").click(function(){
})
$("#id").on("click", 函数 );
$("#id").on("click" , function() {
})
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script>
$(function(){
$("#bid").click(function(){
alert("你还真点!!!");
})
})
</script>
</head>
<body>
<input id="bid" type="button" value="点我试试"/>
</body>
</html>
1.4 常见函数
函数名 | 描述 |
---|
val() 、val(“值”) | 获取值、设置值 |
text | 以文本的方式获取、设置标签体 |
attr(k [, v] ) | 获得、设置标签属性 |
removeAttr(k) | 移除属性 |
append() | 追加 |
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script>
$(function(){
$("#userinput").blur(function(){
var v = $("#userinput").val();
if(v == "jack"){
$("#spanid").text("用户名可用")
$("#bid").removeAttr("disabled")
} else {
$("#spanid").text("用户名不可用")
$("#bid").attr("disabled","disabled");
}
})
})
</script>
</head>
<body>
<input id="userinput" type="text"> <br/>
<span id="spanid"></span> <br/>
<input id="bid" type="button" value="提交">
</body>
</html>
1.5 jquery ajax 标准代码
{
"键1":"值",
"键2":"值"
}
$.ajax({
"url":"请求路径",
"type":"请求方式【GET/POST/PUT/DELETE】",
"contentType":"请求的数据类型【application/json;charset=UTF-8】",
"data": "请求的数据",
"dataType":"响应的数据类型【json】",
"success":"成功的处理函数【function(data){}】",
"error":"失败的处理函数【function(){}】",
})
1.6 各种请求
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script>
$(function(){
$("#bid1").click(function(){
});
$("#bid2").click(function(){
});
$("#bid3").click(function(){
});
$("#bid4").click(function(){
});
$("#bid5").click(function(){
});
})
</script>
</head>
<body>
<input id="bid1" type="button" value="查询所有"> <br/>
<input id="bid2" type="button" value="添加"> <br/>
<input id="bid3" type="button" value="修改"> <br/>
<input id="bid4" type="button" value="删除"> <br/>
<input id="bid5" type="button" value="查询详情"> <br/>
</body>
</html>
$.ajax({
"url": "http://data.javaliang.com/data/jack/student",
"type": "get",
"contentType": "application/json;charset=UTF-8",
"dataType": "json",
"success": function(data){
alert('查询成功')
console.info(data)
},
"error": function(){
alert('查询失败')
}
})
var student = {
"id": "s003",
"name": "李四",
"age" : "18",
"edu" : "大班"
}
$.ajax({
"url":"http://data.javaliang.com/data/jack/student",
"type":"post",
"contentType":"application/json;charset=UTF-8",
"data": JSON.stringify(student) ,
"dataType":"json",
"success": function(data) {
alert('添加成功')
console.info(data)
},
"error":function() {
alert('失败')
}
})
var student = {
"id": "s003",
"name": "李四2",
"age" : "182",
"edu" : "大班2"
}
$.ajax({
"url":"http://data.javaliang.com/data/jack/student",
"type":"put",
"contentType":"application/json;charset=UTF-8",
"data": JSON.stringify(student) ,
"dataType":"json",
"success": function(data) {
alert('修改成功')
console.info(data)
},
"error":function() {
alert('失败')
}
})
$.ajax({
"url":"http://data.javaliang.com/data/jack/student/s003",
"type":"delete",
"contentType":"application/json;charset=UTF-8",
"dataType":"json",
"success": function(data) {
alert('删除成功')
console.info(data)
},
"error":function() {
alert('失败')
}
})