const xhr = new XMLHttpRequest()
xhr.open(type, url)
xhr.onload = (...) => {...}
xhr.send(...)
$id = $_GET[‘id’];
$key = array_search($id,array_column($users.’id’));
echo json_encode($users[$key]);
<button>Ajax.Get request</button>
<p></p>
<script>
const btn = document.querySelector(“button);
btn.onclick = () => {
const xhr = new XMLHttpRequest();
xhr.open(type,”test1.php?id=2”);
}
</script>
### 2. Ajax的get请求
- 创建一个表单,selector
<style>
body {
background-color: pink;
}
.login {
width: 20em;
padding: 0 1em 1em;
background-color: tan;
margin: 2em auto;
display: grid;
place-items: center;
}
.login form {
display: grid;
grid-template-columns: 3em 1fr;
gap: 1em 0;
}
.login form input {
border: none;
outline: none;
}
.login form input:focus,
.login form input:hover {
box.shadow: 0 0 5px lime;
}
.login form button {
background-color: lightgray;
color: white;
outline: none;
border: none;
height: 2em;
}
.login form button:hover {
background-color: darkgray;
cursor: pointer;
box-shadow: 0 0 5px lime;
}
.login form button,
.tips {
grid-area: auto / 2;
}
</style>
</head>
<body>
<div class="login">
<p>Login Name</p>
<form action="" onsubmit="return false">
<label for="email">email:</label>
<input
type="email"
name="email"
id="email"
placeholder="demo@mail.com"
/>
<label form="password">Password</label>
<input
type="password"
name="password"
id="password"
placeholder="minimum 6 "
/>
<button>Submit</button>
<span class="tips"></span>
</form>
<script>
const form = document.querySelector(“.login form”);
const btn = document.querySelector(“.login button”);
const tips = document.querySelector(“.tips”);
- #### Ajax post xhr四步骤
btn.onclick = (ev) => {
ev.preventDefault();
const xhr = new XMLHttpRequest();
xhr.open(“post”, “test2.php”);
xhr.onload = ( => {
console.log(xhr.response);
})
xhr.onerrer = () => console.log(“Error”);
xhr:send(new FormData(form);
### 2. 选顶卡
- 导航tabs,items
<div class="tabs">
<ul class="tab">
<li class="active" data-index="1">Handbags</li>
<li data-index="2">Wallets</li>
<li data-index="3">Shoes</li>
</ul>
<ul data-index="1" class="item active">
<li><a href="">tote bags</a></li>
<li><a href="">crossbody bags</a></li>
<li><a href="">belt bags</a></li>
<li><a href="">mens bags</a></li>
</ul>
<ul data-index="2" class="item">
<li><a href="">clutch long wallets</a></li>
<li><a href="">card holders</a></li>
<li><a href="">short wallets</a></li>
<li><a href="">key pouches</a></li>
</ul>
<ul data-index="3" class="item">
<li><a href="">fashion sneakers </a></li>
<li><a href="">fashion sandals </a></li>
<li><a href="">slids</a></li>
<li><a href="">running shoes</a></li> </ul>
</div>
- 编辑样式style
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
a {
text-decoration: none;
color: #555;
}
a:hover {
text-decoration: underline;
color: red;
}
li {
list-style: none;
line-height: 1.6em;
}
li:hover {
cursor: default;
}
.tabs {
width: 300px;
height: 300px;
margin: 30px;
background-color: #e6e6e6;
display: flex;
flex-direction: column;
}
.tab {
height: 36px;
display: flex;
}
.tab li {
flex: auto;
text-align: center;
line-height: 36px;
background-color: #fff;
}
.tab li.active {
background-color: #e6e6e6;
}
.tab li:hover {
cursor: pointer;
}
.item {
padding: 20px;
display: none;
}
.item.active {
display: block;
}
</style>
- 导航切换
<script>
const tab = document.querySelector(“.tab”);
const items = document.querySelectorAll(“.item”);
tab.onclick = (ev) => {
[…tab.children].forEach((item) => item.classList.remove(“active”));
ev.target.classList.add(“active”);
items.forEach((item) => item.classList.remove(“active”));
[…items]
.filter((item) => item.dataset.index === ev.target.dataset.index)
.pop()
.classList.add(“active”);
};
</script>