
批改狀態(tài):合格
老師批語:很用心
特別提示: 異步請求不要使用
live server
插件,必須創(chuàng)建一個本地服務器環(huán)境
以前端請求,后端響應為例
XMLHttpRequest
對象XMLHttpRequest
是瀏覽器對象,而非 JS 內(nèi)置對象
const xhr = new XMLHttpRequest()
xhr.open(type, url)
xhr.onload = (...) => {...}
xhr.send(...)
序號 | 方法 | 描述 |
---|---|---|
1 | responseType |
設置響應類型 |
2 | response |
響應正文 |
序號 | 方法 | 描述 |
---|---|---|
1 | open(type,url) |
配置請求參數(shù) |
2 | send(data/null) |
發(fā)送請求 |
序號 | 事件 | 描述 |
---|---|---|
1 | load() |
請求成功 |
2 | error() |
請求失敗 |
FormData
對象FormData
是表單數(shù)據(jù)構(gòu)造器
序號 | 方法 | 描述 |
---|---|---|
1 | append(name,value) |
請求成功 |
2 | delete(name) |
請求失敗 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ajax-get請求</title>
</head>
<body>
<button>Ajax-GET請求</button>
<p></p>
<script>
//ajax-get 請求四部曲
// 1. 創(chuàng)建 xhr 對象: `const xhr = new XMLHttpRequest()`
// 2. 配置 xhr 參數(shù): `xhr.open(type, url)`
// 3. 處理 xhr 響應: `xhr.onload = (...) => {...}`
// 4. 發(fā)送 xhr 請求: `xhr.send(...)`
//首先拿到按鈕
const btn = document.querySelector("button");
btn.onclick = () => {
//創(chuàng)建 xhr 對象
const xhr = new XMLHttpRequest();
//配置xhr參數(shù)
xhr.open("get", "test1.php?id=3");
//responseType 響應類型將服務器數(shù)據(jù)解析為JSON對象
xhr.responseType = "json";
//處理xhr響應
xhr.onload = () => {
//response 響應正文
console.log(xhr.response);
//拿到對象里面的name和email值
let user = `${xhr.response.name}+${xhr.response.email}`;
//拿到p標簽,把值傳到p標簽里面去
let p = document.querySelector("p");
p.textContent = user;
};
//發(fā)送xhr請求
xhr.send(null);
};
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ajax-POST</title>
<style>
:root {
background-color: lightblue;
}
.login {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 40px 1fr;
place-items: center;
border: 1px solid;
width: 30rem;
height: 15rem;
margin: auto;
background-color: lightseagreen;
border-radius: 1rem;
}
.login > p {
color: white;
font-size: 20px;
}
.login > form > label {
color: white;
}
.login > form {
display: grid;
grid-template-columns: repeat(1, 3rem 1fr);
grid-template-rows: repeat(4, 1fr);
gap: 3px;
padding: 1rem;
}
.login > form > button {
margin-top: 5px;
width: 13.5rem;
}
.login > form > .tips {
place-items: center;
grid-area: 4 / 2;
color: white;
margin-top: 0.5rem;
}
</style>
</head>
<body>
<div class="login">
<p>用戶登錄</p>
<form action="">
<label for="email">郵箱:</label>
<input
type="email"
name="email"
id="email"
placeholder="admin@qq.com"
/>
<label for="password">密碼:</label>
<input
type="password"
name="password"
id="password"
placeholder="不小于8位數(shù)"
minlength="8"
maxlength="14"
/>
<button>提交</button>
<span class="tips">123123</span>
</form>
</div>
<script>
//首先拿到表單里面的元素
const form = document.querySelector(".login form");
const btn = document.querySelector(".login button");
const tips = document.querySelector(".tips");
// 1. 創(chuàng)建 xhr 對象: `const xhr = new XMLHttpRequest()`
// 2. 配置 xhr 參數(shù): `xhr.open(type, url)`
// 3. 處理 xhr 響應: `xhr.onload = (...) => {...}`
// 4. 發(fā)送 xhr 請求: `xhr.send(...)`
btn.onclick = (ev) => {
//禁用默認行為
ev.preventDefault();
//創(chuàng)建xhr對象
const xhr = new XMLHttpRequest();
//配置xhr參數(shù)
xhr.open("post", "test2.php");
//處理xhr響應
//把服務器驗證正確的數(shù)據(jù)傳遞到tips中
xhr.onload = () => {
tips.innerHTML = xhr.response;
};
//發(fā)送xhr請求,用 new FormData 傳遞表單數(shù)據(jù)
xhr.send(new FormData(form));
};
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>選項卡</title>
<style>
@import url(xxk.css);
</style>
</head>
<body>
<!-- 創(chuàng)建選項卡模板 -->
<div class="xxk">
<!-- 這個是選項卡導航 -->
<ul class="nav">
<!-- 用 data-index自定義屬性來對導航內(nèi)容進行綁定 -->
<li class="active" data-index="1">省內(nèi)</li>
<li data-index="2">國內(nèi)</li>
<li data-index="3">國際</li>
</ul>
<!-- 選項卡導航里面的內(nèi)容 -->
<!-- data-index="1"綁定省內(nèi) -->
<ul data-index="1" class="item active">
<li><a href="">江西已連續(xù)408天無新增本地確診病例...</a></li>
<li><a href="">參與人次破60萬 江西全民國家安全知識答...</a></li>
<li><a href="">河北服毒自殺的貨車司機今晨已下葬 此前...</a></li>
</ul>
<!-- data-index="2"綁定國內(nèi) -->
<ul data-index="2" class="item">
<li><a href="">天津“十四五”將建百萬畝設施農(nóng)業(yè)</a></li>
<li><a href="">保護生物多樣性 守住自然生態(tài)安全邊界</a></li>
<li><a href="">強化現(xiàn)代農(nóng)業(yè)科技和物質(zhì)裝備支撐</a></li>
</ul>
<!-- data-index="3"綁定國際 -->
<ul data-index="3" class="item">
<li><a href="">伊朗原子能組織:納坦茲核設施內(nèi)部供電系統(tǒng)出現(xiàn)故障</a></li>
<li><a href="">日媒:福島核電站4000個廢棄物集裝箱信息不明</a></li>
<li><a href="">美國黑人軍官被兩名白人警察攔下毆打 噴辣椒水</a></li>
</ul>
</div>
<script>
//事件代理實現(xiàn)導航的切換,拿到導航和內(nèi)容
const nav = document.querySelector(".nav");
const items = document.querySelectorAll(".item");
//建立導航事件屬性
nav.onclick = (ev) => {
// console.log(ev.currentTarget);
// console.log(ev.target);
// 1.清空之前的激活樣式,并將導航設置激活狀態(tài)
//將導航用數(shù)組來遍歷
[...nav.children].forEach((item) => item.classList.remove("active"));
ev.target.classList.add("active");
// //2.根據(jù)data-index來確定應該將那個列表進行激活和綁定
items.forEach((item) => item.classList.remove("active"));
[...items]
.filter((item) => item.dataset.index === ev.target.dataset.index)
.pop(0)
.classList.add("active");
};
</script>
</body>
</html>
測試已拿到容器元素,下面我們來給它添加事件屬性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>一件換膚</title>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.box {
width: 300px;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 5px;
margin: 10px auto;
}
.box > img {
width: 100%;
border: 1px solid white;
opacity: 0.6;
}
.box > img:active {
opacity: 1;
}
.box > img:hover {
opacity: 1;
cursor: pointer;
width: 105%;
}
body {
background-image: url(tupian/1.jpeg);
background-size: cover;
background-repeat: no-repeat;
}
</style>
</head>
<body>
<div class="box">
<img src="tupian/1.jpeg" alt="" />
<img src="tupian/2.jpeg" alt="" />
<img src="tupian/3.jpeg" alt="" />
<img src="tupian/4.jpeg" alt="" />
<img src="tupian/5.jpeg" alt="" />
<img src="tupian/6.jpeg" alt="" />
<img src="tupian/7.jpeg" alt="" />
<img src="tupian/8.jpeg" alt="" />
<img src="tupian/9.jpeg" alt="" />
</div>
<script>
//首先拿到容器的事件代理
const box = document.querySelector(".box");
//測試是否拿到
// console.log(box);
box.onclick = function (ev) {
// console.log(box);
//拿到body元素
const body = document.body;
//創(chuàng)建一個點擊的新圖片路徑
let imgSrc = `url('${ev.target.src}')`;
//然后將body里面的圖片給他替換成點擊的圖片背景
body.style.backgroundImage = imgSrc;
};
</script>
</body>
</html>
微信掃碼
關(guān)注PHP中文網(wǎng)服務號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號