
批改狀態(tài):合格
老師批語:作業(yè)中推薦寫一些沒有講到的方法, 這些都在手冊中, 不能只會用學(xué)過的
1.關(guān)于ajax相關(guān)代碼練習(xí)
1.1、登陸表單驗(yàn)證
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="../JQuery3.5.1.js"></script>
<title>登陸表單前端驗(yàn)證</title>
<style>
* {
margin: 0;
padding: 0;
}
h2 {
/* display: block; */
width: 350px;
margin: 0 auto;
text-align: center;
padding-top: 10px;
box-sizing: border-box;
}
form {
margin: 10px auto;
width: 350px;
height: 250px;
background-color: #5384e8;
display: flex;
flex-flow: column nowrap;
justify-content: space-evenly;
align-content: center;
align-items: center;
font-size: 1.2rem;
}
form:hover {
box-shadow: 0 0 5px #626262;
}
form > .button {
width: 280px;
display: flex;
justify-content: space-evenly;
}
form > .button > input {
width: 100px;
height: 30px;
background-color: #00bb00;
border: none;
border-radius: 15px;
}
form > .button > input:hover {
background-color: red;
color: white;
}
a {
color: white;
text-decoration: none;
}
</style>
</head>
<body>
<h2 data-index="one">用戶注冊</h2>
<form method="POST" onsubmit="return false">
<div class="account">
<label for="username">賬戶:</label>
<input
type="email"
required
name="username"
id="username"
placeholder="example@163.com"
autofocus="autofocus"
/>
</div>
<div class="pwd">
<label for="p2">密碼:</label>
<input
type="password"
required
name="p2"
id="p2"
placeholder="不少于六位"
/>
</div>
<div class="button">
<input type="submit" value="登陸" />
<input type="reset" value="重置" />
</div>
<div>
<a href="regist.php">沒有賬號,點(diǎn)擊此處注冊!</a>
</div>
</form>
</body>
<script>
var cl = console.log.bind(console);
// var btu = $('input[type="submit"]');
//禁用表單的默認(rèn)提交事件;
// $("form").submit(function (ev) {
// ev.preventDefault();
// });
$('input[type="email"]').blur(function () {
var tips = "";
var color = "";
// cl(this);
if ($(this).val().length === 0) {
tips = "用戶名不能為空";
color = "red";
}
if (tips.length != 0) {
$(this)
.parent()
.after(
"<span style=' font-size:8px;color:" +
color +
"''>" +
tips +
"</span>"
);
}
});
$("input[type='email']").focus(function () {
cl();
if ($(this).parent().next().get(0).tagName === "SPAN")
$(this).parent().next().get(0).remove();
});
$('input[type="password"]').blur(function () {
var tips = "";
var color = "";
// cl(this);
if ($(this).val().length === 0) {
tips = "密碼不能為空";
color = "red";
} else if ($(this).val().length <= 6) {
cl(tips);
tips = "密碼不能少于6位";
color = "red";
} else {
tips = "";
}
if (tips.length != 0) {
$(this)
.parent()
.after(
"<span style=' font-size:8px;color:" +
color +
"''>" +
tips +
"</span>"
);
}
});
$("input[type='password']").focus(function () {
cl();
if ($(this).parent().next().get(0).tagName === "SPAN")
$(this).parent().next().get(0).remove();
});
</script>
</html>
代碼運(yùn)行結(jié)果:
1.2、JQuery有關(guān)Ajax的代碼練習(xí)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="../JQuery3.5.1.js"></script>
<title>Document</title>
<style>
.Ajax {
display: flex;
flex-flow: column nowrap;
}
</style>
</head>
<body>
<h2>Ajax</h2>
<div class="Ajax">
<button>load()請求數(shù)據(jù)</button>
<button>$.get()請求數(shù)據(jù)</button>
<button>$.post()請求數(shù)據(jù)</button>
<button>$.getJSON請求數(shù)據(jù)</button>
</div>
</body>
<script>
var cl = console.log.bind(console);
//load()導(dǎo)入html片段
$("button")
.first()
.click(function (ev) {
// cl(this);//當(dāng)前觸發(fā)事件的對象,等同于ev.target
// cl(ev.target);//當(dāng)前觸發(fā)事件的對象,等同于this
// cl(ev.currentTarget);//當(dāng)前綁定事件的對象DIV
ev.preventDefault(); //ev當(dāng)前點(diǎn)擊的行為
if ($(this).next().get(0).tagName != "DIV")
$(this).after("<div>").next().load("docment.html");
});
cl($("button").eq(1).text()); //eq(index)index是從零開始的
//$.get()請求
$("button")
.eq(1)
.click(function (ev) {
ev.preventDefault();
// cl(this);
var that = this;
$.get("data.php", "id=3", function (data) {
// cl(data);
$(that).after('<span style="color:red">' + data + "<span>");
});
});
//$.post()請求
$("button")
.eq(2)
.click(function (ev) {
ev.preventDefault();
// cl(this);
var that = this;
$.post("data.php", "id=3", function (data) {
// cl(data);
$(that).after('<span style="color:red">' + data + "<span>");
});
});
//$.getJSON()請求
$("button")
.last()
.click(function (ev) {
ev.preventDefault();
var that = this;
$.getJSON("data.php", "{id:3}", function (data) {
// var data = JSON.parse(data);
// return "你好!";
//要求返回的數(shù)據(jù)必須時(shí)JSON格式
cl(data);
$(that).after('<span style="color:red">' + data.name + "<span>");
});
});
</script>
</html>
代碼運(yùn)行效果圖
1.3 demo2代碼練習(xí)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="../JQuery3.5.1.js"></script>
<style>
.Ajax {
display: flex;
flex-flow: column nowrap;
}
</style>
</head>
<body>
<h2>Ajax</h2>
<div class="Ajax">
<button>$.Ajax()請求數(shù)據(jù)</button>
<button>$.Ajax()-JSONP跨域請求1數(shù)據(jù)</button>
<button>$.Ajax()—JSONP跨域請求2數(shù)據(jù)</button>
</div>
</body>
<script>
var cl = console.log.bind(console);
var first = $("button:first-child");
// cl(first);
//$.ajax()=XMLHttpRequest請求函數(shù)
first.click(function (ev) {
ev.preventDefault();
var that = this;
$.ajax({
type: "GET",
url: "docment.html",
dataType: "html",
success: function (data) {
// cl(data);
cl(that);
$(that).after(data);
},
});
});
//$.ajax()跨域請求
$("button:nth-child(2)").click(function (ev) {
ev.preventDefault();
// var that = this;
$.ajax({
type: "GET",
url: "http://php.edu/cors/demo1.php?jsonp=?&word=hello",
// data: "id=1",
dataType: "jsonp",
jsonpCallback: "index",
});
});
var hello = "hello,China!";
function index(data) {
// cl(data);
// cl(that);
$("button:nth-child(2)").after("<span>" + data + "</span>");
}
$("button:last-child").click(function (ev) {
ev.preventDefault();
var that = this;
$.ajax({
type: "GET",
url: "http://php.edu/cors/demo1.php?jsonp=?&word=hello",
// data: "id=1",
dataType: "jsonp",
success: function (data) {
cl(data);
$(that).after(data);
},
});
});
</script>
</html>
運(yùn)行代碼效果圖
2 無刷新分頁代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>員工信息表</title>
<script src="../JQuery3.5.1.js"></script>
<style>
body > div {
width: 700px;
margin: 0 auto;
outline: 1px solid red;
display: flex;
flex-flow: column nowrap;
align-items: center;
}
p {
height: 25px;
display: flex;
justify-content: flex-start;
align-items: center;
}
p a {
text-decoration: none;
width: 38px;
text-align: center;
margin: 0 3px;
}
.active {
background-color: tomato;
color: white;
}
p a:hover {
cursor: pointer;
}
</style>
</head>
<body>
<div>
<table border="1" cellspacing="0" cellpadding="10">
<caption style="font-size: 28px;">
員工信息表
</caption>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>職位</th>
<th>電話</th>
<th>入職時(shí)間</th>
</tr>
</thead>
<tbody></tbody>
</table>
<p></p>
</div>
</body>
<script>
var cl = console.log.bind(console);
//頁面生成函數(shù)
var page = 1;
get(page);
// 點(diǎn)擊頁面生成新的頁面內(nèi)容下;
$("p").click(function (ev) {
// cl(ev.target);
page = $(ev.target).data("index");
$("tbody").html("");
$("p").html("");
get(page);
});
function get(page) {
$.ajax({
type: "GET",
url: "http://php.edu/pages/contect.php",
data: { p: page },
dataType: "jsonp",
jsonpCallback: "view",
});
}
function view(data) {
// cl(data);
// cl(data.pages);
//生產(chǎn)頁碼
for (var i = 1; i <= data.pages; i++) {
if (i === page) {
$("p").append(
"<a class='active' data-index='" + i + "'>" + i + "</a>"
);
} else {
$("p").append("<a data-index='" + i + "'>" + i + "</a>");
}
}
//生成員工信息表
var staffs = data.data;
// cl(staffs);
var res = "";
staffs.forEach(function (item) {
staff =
"<tr><td>" +
item.id +
"</td><td>" +
item.name +
"</td><td>" +
item.age +
"</td><td>" +
item.sex +
"</td><td>" +
item.position +
"</td><td>" +
item.mobile +
"</td><td>" +
item.hiredate +
"</td></tr>";
// cl(staff);
res += staff;
});
// cl(res);
// cl($("tbody"));
$($("tbody")[0]).html(res);
}
</script>
</html>
運(yùn)行結(jié)果圖
1.JQuery對于事件有關(guān)的函數(shù):
.submit(function(ev){});提交事件函數(shù)
.blur(function(){});失去焦點(diǎn)事件函數(shù)
.focus(function(){});獲取焦點(diǎn)函數(shù)
.keydown()|keyup();鍵盤按鍵函數(shù)
2.$.Ajax()相關(guān)事件函數(shù):
$.load();加載html片段;
$.get(url,data,function(repesondata){});get請求數(shù)據(jù)
$.post(url,data,function(repesondata){});post請求函數(shù)
$.Ajax({type: "GET",url: url,data: data,dataType: "json",success: callback,});通過請求函數(shù)
3.跨域請求函數(shù):$.Ajax({type: "GET",url: url,data: data,dataType: "jsonp",jsonpCallback:函數(shù)名,})
;跨域請求函數(shù)
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號