web APIs
目录
Web APIs第一天
Dom获取&属性操作
Web API基本认知
变量声明
变量声明有三个 var let 和 const
建议: const 优先,尽量使用const,原因是:
- const 语义化更好
- 很多变量我们声明的时候就知道他不会被更改了,那为什么不用 const呢?
- 实际开发中也是,比如react框架,基本const
⭐️有了变量先给const,如果发现它后面是要被修改的,再改为let
以下可以把let改成const:
let arr = ['red','green']
arr.push('pink')
console.log(arr)
let person = {
uname:'pink老师',
age:18,
gender:'女'
}
person.address = '武汉黑马'
console.log(person)
- const 声明的值不能更改,而且const声明变量的时候需要里面进行初始化
- 但是对于引用数据类型,const声明的变量,里面存的不是 值,不是值,不是值,是 地址。
const arr = [1,2,3]
🐱
// 错误,他们地址不一样
const names = []
names = [1,2,3]
const obj = {}
obj = {
uname:'pink老师'
}
// 正确
const names = []
names = [1,2,3]
const obj = {}
obj = {
uname:'pink老师'
}
作用和分类
作用: 就是使用 JS 去操作 html 和浏览器
API分类:DOM (文档对象模型)、BOM(浏览器对象模型)
什么是DOM
- DOM(Document Object Model——文档对象模型)是用来呈现以及与任意 HTML 或 XML文档交互的API
- 白话文:DOM是浏览器提供的一套专门用来 操作网页内容 的功能
- DOM作用:开发网页内容特效和实现用户交互
DOM树
DOM树是什么
将 HTML 文档以树状结构直观的表现出来,我们称之为文档树或 DOM 树
描述网页内容关系的名词
作用:文档树直观的体现了标签与标签之间的关系
DOM对象
DOM对象:浏览器根据html标签生成的 JS对象
- 所有的标签属性都可以在这个对象上面找到
- 修改这个对象的属性会自动映射到标签身上
DOM的核心思想
- 把网页内容当做对象来处理
document 对象
- 是 DOM 里提供的一个对象
- 所以它提供的属性和方法都是用来访问和操作网页内容的,例:document.write()
- 网页所有内容都在document里面
<body>
<div>123</div>
<script>
const div = document.querySelector('div')
// 打印对象
console.dir(div) // dom 对象
</script>
</body>
获取Dom对象
根据CSS选择器来获取DOM元素(重点)
⭐️1.选择匹配的第一个元素
语法:
document.querySelector('css选择器')
参数:
包含一个或多个有效的CSS选择器 字符串
返回值:
CSS选择器匹配的第一个元素,一个 HTMLElement对象。
如果没有匹配到,则返回null。
<body>
<div>123</div>
<div>abc</div>
<script>
// 获取第一个匹配元素
const box = document.querySelector('div')
console.log(box)
</script>
</body>
<body>
<p id="nav">导航栏</p>
<script>
const nav = document.querySelector('#nav')
console.log(nav)
</script>
</body>
<body>
<ul>
<li>测试1</li>
<li>测试2</li>
<li>测试3</li>
</ul>
</script> -->
<script>
const li = document.querySelector('ul li:first-child')
console.log(li)
</script>
</body>
⭐️2.选择匹配的多个元素
语法:
document.querySelectorAll('css选择器')
参数:
包含一个或多个有效的CSS选择器 字符串
返回值:
CSS选择器匹配的NodeList 对象集合
例如:
document.querySelectorAll('ul li')
🐱
得到的是一个伪数组:
- 有长度有索引号的数组
- 但是没有 pop() push() 等数组方法
想要得到里面的每一个对象,则需要遍历(for)的方式获得。
⭕️哪怕只有一个元素,通过querySelectAll() 获取过来的也是一个伪数组,里面只有一个元素而已
其他获取DOM元素方法(了解)
// 根据id获取一个元素
document.getElementById('nav')
// 根据标签获取一类元素获取页所有div
document.getElementsByTagName('div')
// 根据类名获取元素获取页面所有类名为w的
document.getElementsByclassName('w')
操作元素内容
对象.innerText 属性
- 将文本内容添加/更新到任意标签位置
- 显示纯文本,不解析标签
const info = ducument.querySelect('.info')
// 获取标签内部文字
// console.log(info.innerText)
// 添加/修改标签内部文字内容
info.innerText = '你好,我是刘德华'
对象.innerHTML 属性
- 将文本内容添加/更新到任意标签位置
- 会解析标签,多标签建议使用模板字符
const info = ducument.querySelect('.info')
// 获取标签内部文字
// console.log(info.innerHTML)
// 添加/修改标签内部文字内容
info.innerHTML = `你好,我是<strong>刘德华</strong>`
年会抽奖案例
<!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>
.wrapper {
width: 840px;
height: 420px;
background: url(./images/bg01.jpg) no-repeat center / cover;
padding: 100px 250px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="wrapper">
<strong>传智教育年会抽奖</strong>
<h1>一等奖:<span id="one">???</span></h1>
<h3>二等奖:<span id="two">???</span></h3>
<h5>三等奖:<span id="three">???</span></h5>
</div>
<script>
const personArr = ['周杰伦', '刘德华', '周星驰', 'Pink老师', '张学友']
// 一等奖
const random1 = Math.floor(Math.random() * personArr.length)
const one = document.querySelector('#one')
one.innerHTML = personArr[random1]
personArr.splice(random1, 1)
// 二等奖
const random2 = Math.floor(Math.random() * personArr.length)
const two = document.querySelector('#two')
two.innerHTML = personArr[random2]
personArr.splice(random2, 1)
// 三等奖
const random3 = Math.floor(Math.random() * personArr.length)
const three = document.querySelector('#three')
three.innerHTML = personArr[random3]
personArr.splice(random3, 1)
</script>
</body>
</html>
操作元素属性
操作元素常用属性
- 可以通过 JS 设置/修改标签元素属性,比如通过 src更换 图片
- 最常见的属性比如: href、title、src 等
⭐️语法:
对象.属性 = 值
🌰举例说明:
// 1.获取元素
const img = document.querySelector('img')
// 2.修改元素
img.src = './images/2.webp'
img.title = '刘德华'
操作元素样式属性
可以通过 JS 设置/修改标签元素的样式属性。
- 比如通过 轮播图小圆点自动更换颜色样式
- 点击按钮可以滚动图片,这是移动的图片的位置 left 等等
⭐️1.通过 style 属性操作CSS
语法:
对象.style.样式属性 = 值
🌰举例说明:
const box = document.querySelector('.box')
// 修改元素样式
box.style.width = '200px'
box.style.marginTop = '15px' // margin-top
box.style.backgroundColor = 'pink' // background-color
⭕️
- 修改样式通过style属性引出
- 如果属性有-连接符,需要转换为小驼峰命名法
- 赋值的时候,需要的时候不要忘记加css单位
☁️练习:页面刷新,页面随机更换背景图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
background: url(./images/desktop_1.jpg) no-repeat top center/cover;
}
</style>
</head>
<body>
<script>
function getRandom(N, M) {
return Math.floor(Math.random() * (M - N + 1)) + N
}
const random = getRandom(1, 10)
document.body.style.backgroundImage = `url(./images/desktop_${random}.jpg)`
</script>
</body>
</html>
⭐️2.操作类名(className) 操作CSS
操作类名(className) 操作CSS
如果修改的样式比较多,直接通过style属性修改比较繁琐,我们可以通过借助于css类名的形式。
语法:
// active 是一个css类名
元素.className = 'active'
⭕️
- 由于class是关键字, 所以使用className去代替
- className是使用新值换旧值, 如果需要添加一个类,需要保留之前的类名
🌰例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 200px;
height: 200px;
background-color: pink;
}
.box {
width: 300px;
height: 300px;
background-color: skyblue;
margin: 100px auto;
padding: 10px;
border: black
}
</style>
</head>
<body>
<div></div>
<script>
// 1.获取元素
const div = document.querySelector('div')
// 2.添加类名 class 是个关键字 我们用className
div.className = 'box'
</script>
</body>
</html>
⭐️3.通过 classList 操作类控制CSS
为了解决className 容易覆盖以前的类名,我们可以通过classList方式追加和删除类名
语法:
// 追加一个类
元素.classList.add('类名')
// 删除一个类
元素.classList.remove('类名')
// 切换一个类
元素.classList.toggle('类名')
🌰例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 200px;
height: 200px;
}
.ative {
color: red;
background-color: pink;
}
</style>
</head>
<body>
<div class='box'>文字</div>
<script>
// 1.获取元素
const box = document.querySelector('.box')
// 2.修改样式
// 2.1 追加类 add() 类名不加点,并且是字符串
box.classList.add('ative')
// 2.2 删除类 remove() 类名不加点,并且是字符串
box.classList.remove('box')
// 2.3 切换类 toggle() 有就删掉,没有就加上
box.classList.toggle('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>
* {
box-sizing: border-box;
}
.slider {
width: 560px;
height: 400px;
overflow: hidden;
}
.slider-wrapper {
width: 100%;
height: 320px;
}
.slider-wrapper img {
width: 100%;
height: 100%;
display: block;
}
.slider-footer {
height: 80px;
background-color: rgb(100, 67, 68);
padding: 12px 12px 0 12px;
position: relative;
}
.slider-footer .toggle {
position: absolute;
right: 0;
top: 12px;
display: flex;
}
.slider-footer .toggle button {
margin-right: 12px;
width: 28px;
height: 28px;
appearance: none;
border: none;
background: rgba(255, 255, 255, 0.1);
color: #fff;
border-radius: 4px;
cursor: pointer;
}
.slider-footer .toggle button:hover {
background: rgba(255, 255, 255, 0.2);
}
.slider-footer p {
margin: 0;
color: #fff;
font-size: 18px;
margin-bottom: 10px;
}
.slider-indicator {
margin: 0;
padding: 0;
list-style: none;
display: flex;
align-items: center;
}
.slider-indicator li {
width: 8px;
height: 8px;
margin: 4px;
border-radius: 50%;
background: #fff;
opacity: 0.4;
cursor: pointer;
}
.slider-indicator li.active {
width: 12px;
height: 12px;
opacity: 1;
}
</style>
</head>
<body>
<div class="slider">
<div class="slider-wrapper">
<img src="./images/slider01.jpg" alt="" />
</div>
<div class="slider-footer">
<p>对人类来说会不会太超前了?</p>
<ul class="slider-indicator">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="toggle">
<button class="prev"><</button>
<button class="next">></button>
</div>
</div>
</div>
<script>
// 1. 初始数据
const sliderData = [
{ url: './images/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },
{ url: './images/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },
{ url: './images/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },
{ url: './images/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },
{ url: './images/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },
{ url: './images/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },
{ url: './images/slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },
{ url: './images/slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },
]
// 1.需要一个随机数
const random = parseInt(Math.random() * sliderData.length)
// console.log(sliderData[random])
// 2.把对应的数据渲染到标签里面
// 2.1 获取图片
const img = document.querySelector('.slider-wrapper img')
// 2.2 修改图片路径 = 对象.url
img.src = sliderData[random].url
// 3. 把p里面的文字内容更换
// 3.1 获取p
const p = document.querySelector('.slider-footer p')
// 3.2 修改p
p.innerHTML = sliderData[random].title
// 4.修改背景颜色
const sf = document.querySelector('.slider-footer')
sf.style.backgroundColor = sliderData[random].color
// 5.小圆点
const li = document.querySelector(`.slider-indicator li:nth-child(${random + 1})`)
li.classList.add('active')
</script>
</body>
</html>
操作表单元素属性
获取: DOM对象.属性名
设置: DOM对象.属性名 = 新值
表单.value = '用户名'
表单.type = 'password'
- 表单属性中添加就有效果,移除就没有效果,一律使用布尔值表示 如果为true 代表添加了该属性 如果是false 代表移除了该属性
- 比如: disabled、checked、selected
<body>
<input type="checkbox">
<button>60秒后再次获取验证码</button>
<script>
// checked
const ipt = document.querySelector('input')
ipt.checked = true
// ipt.checked = 'true' // 会选中,不提倡 有隐式转换
// disabled
const button = document.querySelector('button')
// console.log(button.disabled) // 默认false 不禁用
button.disabled = true
</script>
</body>
自定义属性
标准属性: 标签天生自带的属性 比如class id title等, 可以直接使用点语法操作比如: disabled、checked、
selected
自定义属性:
- 在html5中推出来了专门的data-自定义属性
- 在标签上一律以data-开头
- 在DOM对象上一律以dataset对象方式获取
<body>
<div class="box" data-id="10">盒子</div>
<script>
const box = document.querySelector('.box')
console.log(box.dataset.id)
</script>
</body>
定时器-间歇函数
定时器函数介绍
- 网页中经常会需要一种功能:每隔一段时间需要自动执行一段代码,不需要我们手动去触发
- 例如:网页中的倒计时
- 要实现这种需求,需要定时器函数
- 定时器函数有两种,其中一种是间歇函数
定时器函数基本使用
定时器函数可以开启和关闭定时器
⭐️1. 开启定时器
setInterval(函数,间隔时间)
- 作用:每隔一段时间调用这个函数
- 间隔时间单位是毫秒
🌰举例说明:
// setInterval(函数名,间隔时间)
setInterval(function () {
console.log('一秒钟执行一次')
}, 1000)
function fn() {
console.log('一秒执行一次')
}
setInterval(fn, 1000)
⭕️
- 函数名字不需要加括号
- 定时器返回的是一个id数字
⭐️2. 关闭定时器
let 变量名 = setInterval(函数,间隔时间)
clearInterval(变量名)
🌰
let timer = setInterval(function () {
console.log('一秒钟执行一次')
}, 1000)
clearInterval(timer)
一般不会刚创建就停止,而是满足一定条件再停止
☁️案例:阅读注册协议
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<textarea name="" id="" cols="30" rows="10">
用户注册协议
欢迎注册成为京东用户!在您注册过程中,您需要完成我们的注册流程并通过点击同意的形式在线签署以下协议,请您务必仔细阅读、充分理解协议中的条款内容后再点击同意(尤其是以粗体或下划线标识的条款,因为这些条款可能会明确您应履行的义务或对您的权利有所限制)。
【请您注意】如果您不同意以下协议全部或任何条款约定,请您停止注册。您停止注册后将仅可以浏览我们的商品信息但无法享受我们的产品或服务。如您按照注册流程提示填写信息,阅读并点击同意上述协议且完成全部注册流程后,即表示您已充分阅读、理解并接受协议的全部内容,并表明您同意我们可以依据协议内容来处理您的个人信息,并同意我们将您的订单信息共享给为完成此订单所必须的第三方合作方(详情查看
</textarea>
<br>
<button class="btn" disabled>我已经阅读用户协议(5)</button>
<script>
// 1.获取元素
const btn = document.querySelector('.btn')
// 2.开启定时器
let i = 5
let n = setInterval(function () {
i--
btn.innerHTML = `我已经阅读用户协议(${i})`
if (i === 0) {
clearInterval(n) // 3.关闭定时器
btn.disabled = false
btn.innerHTML = '同意'
}
}, 1000)
</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>
* {
box-sizing: border-box;
}
.slider {
width: 560px;
height: 400px;
overflow: hidden;
}
.slider-wrapper {
width: 100%;
height: 320px;
}
.slider-wrapper img {
width: 100%;
height: 100%;
display: block;
}
.slider-footer {
height: 80px;
background-color: rgb(100, 67, 68);
padding: 12px 12px 0 12px;
position: relative;
}
.slider-footer .toggle {
position: absolute;
right: 0;
top: 12px;
display: flex;
}
.slider-footer .toggle button {
margin-right: 12px;
width: 28px;
height: 28px;
appearance: none;
border: none;
background: rgba(255, 255, 255, 0.1);
color: #fff;
border-radius: 4px;
cursor: pointer;
}
.slider-footer .toggle button:hover {
background: rgba(255, 255, 255, 0.2);
}
.slider-footer p {
margin: 0;
color: #fff;
font-size: 18px;
margin-bottom: 10px;
}
.slider-indicator {
margin: 0;
padding: 0;
list-style: none;
display: flex;
align-items: center;
}
.slider-indicator li {
width: 8px;
height: 8px;
margin: 4px;
border-radius: 50%;
background: #fff;
opacity: 0.4;
cursor: pointer;
}
.slider-indicator li.active {
width: 12px;
height: 12px;
opacity: 1;
}
</style>
</head>
<body>
<div class="slider">
<div class="slider-wrapper">
<img src="./images/slider01.jpg" alt="" />
</div>
<div class="slider-footer">
<p>对人类来说会不会太超前了?</p>
<ul class="slider-indicator">
<li class="active"></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="toggle">
<button class="prev"><</button>
<button class="next">></button>
</div>
</div>
</div>
<script>
// 1. 初始数据
const sliderData = [
{ url: './images/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },
{ url: './images/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },
{ url: './images/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },
{ url: './images/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },
{ url: './images/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },
{ url: './images/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },
{ url: './images/slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },
{ url: './images/slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },
]
// 1.获取元素
const img = document.querySelector('.slider-wrapper img')
const p = document.querySelector('.slider-footer p')
const sf = document.querySelector('.slider-footer')
let i = 0
// 2.开启定时器
setInterval(function () {
i++
img.src = sliderData[i].url // 更改图片路径
p.innerHTML = sliderData[i].title // 把字写到p里
sf.style.backgroundColor = sliderData[i].color // 更改背景颜色
// 小圆点
// 先删除以前的active
document.querySelector('.slider-indicator .active').classList.remove('active')
// 只让当前的li添加active
document.querySelector(`.slider-indicator li:nth-child(${i + 1})`).classList.add('active')
if (i >= sliderData.length - 1) {
i = -1
}
}, 1000)
</script>
</body>
</html>
原文地址:https://blog.csdn.net/2301_76979886/article/details/143025240
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!