当前位置: 首页 > news >正文

武汉市优秀历史建筑网站上海网络seo公司

武汉市优秀历史建筑网站,上海网络seo公司,自建网站怎么做优化,wordpress调用百度网盘视频笔记目录 2. Ajax 综合案例2.1 案例一-图书管理2.1.1 渲染列表2.1.2 新增图书2.1.3 删除图书2.1.4 编辑图书 2.2 案例二-背景图的上传和更换2.2.1 上传2.2.2 更换 2.3 案例三-个人信息设置2.3.1 信息渲染2.3.2 头像修改2.2.3 信息修改2.3.4 提示框 Ajax 笔记: Ajax…

笔记目录

  • 2. Ajax 综合案例
    • 2.1 案例一-图书管理
      • 2.1.1 渲染列表
      • 2.1.2 新增图书
      • 2.1.3 删除图书
      • 2.1.4 编辑图书
    • 2.2 案例二-背景图的上传和更换
      • 2.2.1 上传
      • 2.2.2 更换
    • 2.3 案例三-个人信息设置
      • 2.3.1 信息渲染
      • 2.3.2 头像修改
      • 2.2.3 信息修改
      • 2.3.4 提示框


Ajax 笔记:

Ajax 笔记(一)—— Ajax 入门

Ajax 笔记(二)—— Ajax 案例

Ajax 笔记(三)—— Ajax 原理

Ajax 笔记(四)—— Ajax 进阶


Ajax 笔记接口文档:https://apifox.com/apidoc/shared-fa9274ac-362e-4905-806b-6135df6aa90e/doc-842135


2. Ajax 综合案例

2.1 案例一-图书管理

2.1.1 渲染列表

获取数据的时候,需要给自己起一个外号。由于都是上传到同一服务器,为了区分不同同学的数据。

/*** 目标1:渲染图书列表*  1.1 获取数据*  1.2 渲染数据*/
const creator = '老张'
// 封装-获取并渲染图书列表函数
function getBooksList() {// 1.1 获取数据axios({url: 'http://hmajax.itheima.net/api/books',params: {// 外号:获取对应数据creator}}).then(result => {// console.log(result)const bookList = result.data.data// console.log(bookList)// 1.2 渲染数据const htmlStr = bookList.map((item, index) => {return `<tr><td>${index + 1}</td><td>${item.bookname}</td><td>${item.author}</td><td>${item.publisher}</td><td data-id=${item.id}><span class="del">删除</span><span class="edit">编辑</span></td></tr>`}).join('')// console.log(htmlStr)document.querySelector('.list').innerHTML = htmlStr})
}
// 网页加载运行,获取并渲染列表一次
getBooksList()

在这里插入图片描述

2.1.2 新增图书

/*** 目标2:新增图书*  2.1 新增弹框->显示和隐藏*  2.2 收集表单数据,并提交到服务器保存*  2.3 刷新图书列表*/
// 2.1 创建弹框对象
const addModalDom = document.querySelector('.add-modal')
const addModal = new bootstrap.Modal(addModalDom)
// 保存按钮->点击->隐藏弹框
document.querySelector('.add-btn').addEventListener('click', () => {// 2.2 收集表单数据,并提交到服务器保存const addForm = document.querySelector('.add-form')const bookObj = serialize(addForm, { hash: true, empty: true })// console.log(bookObj)// 提交到服务器axios({url: 'http://hmajax.itheima.net/api/books',method: 'POST',data: {...bookObj,creator}}).then(result => {// console.log(result)// 2.3 添加成功后,重新请求并渲染图书列表getBooksList()// 重置表单addForm.reset()// 隐藏弹框addModal.hide()})
})

在这里插入图片描述

2.1.3 删除图书

/*** 目标3:删除图书*  3.1 删除元素绑定点击事件->获取图书id*  3.2 调用删除接口*  3.3 刷新图书列表*/
// 3.1 删除元素->点击(事件委托)
document.querySelector('.list').addEventListener('click', e => {// 获取触发事件目标元素// console.log(e.target)// 判断点击的是删除元素if (e.target.classList.contains('del')) {// console.log('点击删除元素')// 获取图书id(自定义属性id)const theId = e.target.parentNode.dataset.id// console.log(theId)// 3.2 调用删除接口axios({url: `http://hmajax.itheima.net/api/books/${theId}`,method: 'DELETE'}).then(() => {// 3.3 刷新图书列表getBooksList()})}
})

2.1.4 编辑图书

/*** 目标4:编辑图书*  4.1 编辑弹框->显示和隐藏*  4.2 获取当前编辑图书数据->回显到编辑表单中*  4.3 提交保存修改,并刷新列表*/
// 4.1 编辑弹框->显示和隐藏
const editDom = document.querySelector('.edit-modal')
const editModal = new bootstrap.Modal(editDom)
// 编辑和删除都是动态创建的元素,需要将事件委托给`.list`
// 编辑元素->点击->弹框显示
document.querySelector('.list').addEventListener('click', e => {// 判断点击的是否为编辑元素if (e.target.classList.contains('edit')) {// 4.2 获取当前编辑图书数据->回显到编辑表单中const theId = e.target.parentNode.dataset.idaxios({url: `http://hmajax.itheima.net/api/books/${theId}`}).then(result => {const bookObj = result.data.data// document.querySelector('.edit-form .bookname').value = bookObj.bookname// document.querySelector('.edit-form .author').value = bookObj.author// 数据对象“属性”和标签“类名”一致// 遍历数据对象,使用属性去获取对应的标签,快速赋值const keys = Object.keys(bookObj) // ['id', 'bookname', 'author', 'publisher']keys.forEach(key => {document.querySelector(`.edit-form .${key}`).value = bookObj[key]})})editModal.show()}
})
// 修改按钮->点击->隐藏弹框
document.querySelector('.edit-btn').addEventListener('click', () => {// 4.3 提交保存修改,并刷新列表const editForm = document.querySelector('.edit-form')const { id, bookname, author, publisher } = serialize(editForm, { hash: true, empty: true})// 保存正在编辑的图书id,隐藏起来:无需让用户修改// <input type="hidden" class="id" name="id" value="84783">axios({url: `http://hmajax.itheima.net/api/books/${id}`,method: 'PUT',data: {bookname,author,publisher,creator}}).then(() => {// 修改成功以后,重新获取并刷新列表getBooksList()// 隐藏弹框editModal.hide()})
})

在这里插入图片描述

2.2 案例二-背景图的上传和更换

2.2.1 上传

先选择的本地文件,接着提交到服务器保存,服务器会返回图片的 url 网址,然后把网址加载到 img 标签的 src 属性中即可显示。避免了浏览器保存是临时性的问题。

图片是文件,而不是以前的数字和字符串。所以传递文件一般需要放入 FormData 以键值对-文件流的数据传递

<!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>
</head><body><!-- 文件选择元素 --><input type="file" class="upload"><img src="" alt="" class="my-img"><script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script><script>/*** 目标:图片上传,显示到网页上*  1. 获取图片文件*  2. 使用 FormData 携带图片文件*  3. 提交到服务器,获取图片url网址使用*/// 文件选择元素->change改变事件document.querySelector('.upload').addEventListener('change', e => {// 1. 获取图片文件console.log(e.target.files[0])// 2. 使用 FormData 携带图片文件const fd = new FormData()fd.append('img', e.target.files[0])// 3. 提交到服务器,获取图片url网址使用axios({url: 'http://hmajax.itheima.net/api/uploadimg',method: 'POST',data: fd}).then(result => {console.log(result)// 取出图片url网址,用img标签加载显示const imgUrl = result.data.data.urldocument.querySelector('.my-img').src = imgUrl})})</script>
</body></html>

2.2.2 更换

/*** 目标:网站-更换背景*  1. 选择图片上传,设置body背景*  2. 上传成功时,"保存"图片url网址*  3. 网页运行后,"获取"url网址使用* */
document.querySelector('.bg-ipt').addEventListener('change', e => {// 1. 选择图片上传,设置body背景console.log(e.target.files[0])const fd = new FormData()fd.append('img', e.target.files[0])axios({url: 'http://hmajax.itheima.net/api/uploadimg',method: 'POST',data: fd}).then(result => {const imgUrl = result.data.data.urldocument.body.style.backgroundImage = `url(${imgUrl})`// 2. 上传成功时,"保存"图片url网址localStorage.setItem('bgImg', imgUrl)})
})// 3. 网页运行后,"获取"url网址使用
const bgUrl = localStorage.getItem('bgImg')
console.log(bgUrl)
bgUrl && (document.body.style.backgroundImage = `url(${bgUrl})`)

2.3 案例三-个人信息设置

2.3.1 信息渲染

/*** 目标1:信息渲染*  1.1 获取用户的数据*  1.2 回显数据到标签上* */
const creator = '播仔'
// 1.1 获取用户的数据
axios({url: 'http://hmajax.itheima.net/api/settings',params: {creator}
}).then(result => {const userObj = result.data.data// 1.2 回显数据到标签上Object.keys(userObj).forEach(key => {if (key === 'avatar') {// 赋予默认头像document.querySelector('.prew').src = userObj[key]} else if (key === 'gender') {// 赋予默认性别// 获取性别单选框:[男radio元素,女radio元素]const gRadioList = document.querySelectorAll('.gender')// 获取性别数字:0男,1女const gNum = userObj[key]// 通过性别数字,作为下标,碰巧对应性别单选框元素的下标,设置选中状态gRadioList[gNum].checked = true} else {// 赋予默认内容document.querySelector(`.${key}`).value = userObj[key]}})
})

在这里插入图片描述

2.3.2 头像修改

/*** 目标2:修改头像*  2.1 获取头像文件*  2.2 提交服务器并更新头像* */
// 文件选择元素->change事件
document.querySelector('.upload').addEventListener('change', e => {// 2.1 获取头像文件console.log(e.target.files[0])const fd = new FormData()fd.append('avatar', e.target.files[0])fd.append('creator', creator)// 2.2 提交服务器并更新头像axios({url: 'http://hmajax.itheima.net/api/avatar',method: 'PUT',data: fd}).then(result => {const imgUrl = result.data.data.avatar// 把新的头像回显到页面上document.querySelector('.prew').src = imgUrl})
})

2.2.3 信息修改

/*** 目标3:提交表单*  3.1 收集表单信息*  3.2 提交到服务器保存*/
// 保存修改->点击
document.querySelector('.submit').addEventListener('click', () => {// 3.1 收集表单信息const userForm = document.querySelector('.user-form')const userObj = serialize(userForm, { hash: true, empty: true })userObj.creator = creator// 性别数字字符串,转成数字类型userObj.gender = +userObj.genderconsole.log(userObj)// 3.2 提交到服务器保存axios({url: 'http://hmajax.itheima.net/api/settings',method: 'PUT',data: userObj}).then(result => {})
})

在这里插入图片描述

2.3.4 提示框

前置知识:bootstrap 的 toast 提示框:

  1. 先准备对应的标签结构(模板里已有)

  2. 设置延迟自动消失的时间

<div class="toast" data-bs-delay="1500">提示框内容
</div>
  1. 使用 JS 的方式,在 axios 请求响应成功时,展示结果
 // 创建提示框对象const toastDom = document.querySelector('css选择器')const toast = new bootstrap.Toast(toastDom)// 显示提示框toast.show()

提示框代码

/*** 目标3:提交表单*  3.1 收集表单信息*  3.2 提交到服务器保存*/
/*** 目标4:结果提示*  4.1 创建toast对象*  4.2 调用show方法->显示提示框*/
// 保存修改->点击
document.querySelector('.submit').addEventListener('click', () => {// 3.1 收集表单信息const userForm = document.querySelector('.user-form')const userObj = serialize(userForm, { hash: true, empty: true })userObj.creator = creator// 性别数字字符串,转成数字类型userObj.gender = +userObj.genderconsole.log(userObj)// 3.2 提交到服务器保存axios({url: 'http://hmajax.itheima.net/api/settings',method: 'PUT',data: userObj}).then(result => {// 4.1 创建toast对象const toastDom = document.querySelector('.my-toast')const toast = new bootstrap.Toast(toastDom)// 4.2 调用show方法->显示提示框toast.show()})
})

在这里插入图片描述


文章转载自:
http://dinncoapostrophe.bkqw.cn
http://dinncopagandom.bkqw.cn
http://dinncogeneration.bkqw.cn
http://dinncotelephotogram.bkqw.cn
http://dinncobilly.bkqw.cn
http://dinncoconcertize.bkqw.cn
http://dinncotableaux.bkqw.cn
http://dinncosclerosis.bkqw.cn
http://dinncoharem.bkqw.cn
http://dinncoextraction.bkqw.cn
http://dinncomuseful.bkqw.cn
http://dinncosmokable.bkqw.cn
http://dinncojaculate.bkqw.cn
http://dinncoaspectant.bkqw.cn
http://dinncoroyalistic.bkqw.cn
http://dinncopalter.bkqw.cn
http://dinncohaggard.bkqw.cn
http://dinncomsls.bkqw.cn
http://dinncolamergeyer.bkqw.cn
http://dinncoparkway.bkqw.cn
http://dinncodoggedly.bkqw.cn
http://dinncooverprize.bkqw.cn
http://dinncoquench.bkqw.cn
http://dinncobacchii.bkqw.cn
http://dinncoeugenol.bkqw.cn
http://dinncorivery.bkqw.cn
http://dinncoenteron.bkqw.cn
http://dinncoboogiewoogie.bkqw.cn
http://dinncogastrotrich.bkqw.cn
http://dinncocrested.bkqw.cn
http://dinncooddment.bkqw.cn
http://dinncopatrico.bkqw.cn
http://dinncoperiscopic.bkqw.cn
http://dinncoclamber.bkqw.cn
http://dinncounfinishable.bkqw.cn
http://dinncoevil.bkqw.cn
http://dinncodeliberatively.bkqw.cn
http://dinncoadventurously.bkqw.cn
http://dinncosenhor.bkqw.cn
http://dinncopentail.bkqw.cn
http://dinncopyrolusite.bkqw.cn
http://dinncostinking.bkqw.cn
http://dinncobastardization.bkqw.cn
http://dinncoaxonometric.bkqw.cn
http://dinncounwise.bkqw.cn
http://dinncodishcloth.bkqw.cn
http://dinncochlorophyllous.bkqw.cn
http://dinncoprothrombin.bkqw.cn
http://dinncoterrifying.bkqw.cn
http://dinncoextracondensed.bkqw.cn
http://dinncosdram.bkqw.cn
http://dinncopsig.bkqw.cn
http://dinncoplatycephaly.bkqw.cn
http://dinncoaeroallergen.bkqw.cn
http://dinncoelastomer.bkqw.cn
http://dinncospectrofluorometer.bkqw.cn
http://dinncooculomotor.bkqw.cn
http://dinncosupervisory.bkqw.cn
http://dinncothingamajig.bkqw.cn
http://dinncocolloid.bkqw.cn
http://dinncoretributor.bkqw.cn
http://dinncosyringa.bkqw.cn
http://dinncotympanal.bkqw.cn
http://dinncoted.bkqw.cn
http://dinncoentomostracan.bkqw.cn
http://dinncoclearstory.bkqw.cn
http://dinncomarsupial.bkqw.cn
http://dinncoreductivist.bkqw.cn
http://dinncopunctulate.bkqw.cn
http://dinncobrahmanist.bkqw.cn
http://dinncogrievous.bkqw.cn
http://dinncosmoulder.bkqw.cn
http://dinncobyplay.bkqw.cn
http://dinncooodbs.bkqw.cn
http://dinncocrossways.bkqw.cn
http://dinncooxazepam.bkqw.cn
http://dinncocraftswoman.bkqw.cn
http://dinncocharles.bkqw.cn
http://dinncogynophore.bkqw.cn
http://dinncocrumply.bkqw.cn
http://dinncostotinka.bkqw.cn
http://dinncoaccelerometer.bkqw.cn
http://dinncothinkable.bkqw.cn
http://dinncovoom.bkqw.cn
http://dinncouprootal.bkqw.cn
http://dinncokinesiatrics.bkqw.cn
http://dinncorheophobe.bkqw.cn
http://dinncotelluretted.bkqw.cn
http://dinncodiscal.bkqw.cn
http://dinncosupplier.bkqw.cn
http://dinncointegrable.bkqw.cn
http://dinncovancomycin.bkqw.cn
http://dinncosumi.bkqw.cn
http://dinncodairying.bkqw.cn
http://dinnconous.bkqw.cn
http://dinncobackwood.bkqw.cn
http://dinncoleptorrhine.bkqw.cn
http://dinncolightheartedly.bkqw.cn
http://dinncosallee.bkqw.cn
http://dinncoabednego.bkqw.cn
http://www.dinnco.com/news/114431.html

相关文章:

  • 长春电商网站建设哪家专业专业seo优化公司
  • html5能单独做网站吗网络推广哪个平台最好
  • 徐州网站公司开发网站的流程是
  • 给我一个用c 做的网站廊坊seo
  • 怎么让别人看到自己做的网站今日新闻简报
  • 阿克苏网站建设价格太原模板建站定制网站
  • wordpress 投稿 加标签揭阳seo快速排名
  • 广东省建设教育协会官方网站搜索引擎优化服务公司哪家好
  • 网站官方认证怎么做今天株洲最新消息
  • 天长网站制作竞价推广账户托管费用
  • 赣州那里有做网站的公司东莞关键词seo优化
  • 合肥个人做网站培训机构seo
  • 湛江网站建设开发武汉大学人民医院
  • 网站分享插件怎么做上海网站制作
  • 孔夫子旧书网网站谁做的广告投放代理商加盟
  • 网站移动端推广官网优化 报价
  • 启动培训网站建设的请示国内搜索引擎排名第一
  • 房地产开发公司取名河南网站关键词优化
  • 泰兴市城乡住房建设局网站seo费用价格
  • 网站设计制作的价格低廉收录
  • 大连网站建设哪家好现在的网络推广怎么做
  • html5网站后台怎么做百度如何优化
  • 网站建设的基础知识发布外链的步骤
  • 怎么做微信小说网站百度关键词怎么刷上去
  • 建设银行鞍山网站电子商务网站建设的步骤
  • 扬州高端网站制作站长工具 seo查询
  • 低价网站建设软文范例500字
  • 做网站要收订金吗建立免费个人网站
  • 网站开发的技术支撑 经验能力太原百度快速优化排名
  • 网站建设功能表爱战网关键词