1. Ajax的get请求

  • xhr 请求4步骤

  1. 创建 xhr 对象: const xhr = new XMLHttpRequest()
  2. 配置 xhr 参数: xhr.open(type, url)
  3. 处理 xhr 响应: xhr.onload = (...) => {...}
  4. 发送 xhr 请求: xhr.send(...)
  • 创建test1.php文本

    <?php
    $users = [
    [‘id’=>1, ‘name’=>’Peter’, ‘email’=>’peter@abc.com’, ‘password’=> md5(‘123456’)],
    [‘id’=>2, ‘name’=>’Joe’, ‘email’=>’joe@abc.com’,’password’=> md5(‘abc123’)],
    [‘id’=>3, ‘name’=>’Mary’,’email’=>’mary@abc.com’,’password’ => md5(‘abc456’)],
    ];

$id = $_GET[‘id’];
$key = array_search($id,array_column($users.’id’));
echo json_encode($users[$key]);

  • 配置:输入请求的脚本和查询条件代替url

    xhr.open(type,”test1.php?id=2”);
  • 处理 xhr 响应

    xhr.onload = () => {
    console.log(xhr.response);};
    xhr.onerror = () => console.log(object);
  • 发送 xhr 请求

    xhr.send(null)

<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>

更多相关文章

  1. 【实战案例】Ajax请求第三方接口数据利用模板引擎渲染天气预报页
  2. ajax 的 get, post 请求;实战:选项卡和一键换肤----0408
  3. 通过lua将nginx请求状态码转变为自定义状态码
  4. vue前端一直发生websocket请求的解决办法
  5. 使用除按钮标签(button,submit)以外的标签实现以POST方式发送请求
  6. Servlet过滤器使用实例(防止用户恶意登录)
  7. springcloud组件zuul报Forwarding error问题的解决
  8. tomcat环境部署
  9. jQuery常用dom操作与vue基础

随机推荐

  1. 使用纯HTML的通用数据管理和服务
  2. 手把手教你制作Google Sitemap(详细制作
  3. 在XPath查询中指定轴(转自MSSQL手册)
  4. 使用XSLT将XML数据转换成HTML
  5. Google Sitemap原来支持普通Feed
  6. 使用带批注的 XDR 架构创建 XML 视图
  7. 利用xslt对xml进行缩进格式化处理
  8. 用XsltArgumentList实现xsl的参数调用
  9. 使用 XML 模板 (MSSQL手册)
  10. 多级联动下拉选择框,动态获取下一级