01-DOM节点:网页一切内容皆节点
1.1-DOM节点介绍
1.什么是节点:在HTML文档中,一切皆节点(HTML文档本身、标签、属性、注释内容、文本)
- 2.什么是元素:元素在HTML中叫做标签,在JS的dom对象中称为元素(可以理解为标签的面向对象的叫法)
- 3.HTML标签属于节点的一种,叫做元素节点
- 4.节点三要素:
- 节点类型:标签、属性、注释、文本
- 节点名称:p、div、class(标签名)
- 节点的值:one(属性的值)
- 在JavaScript中,document这个对象大家一定很熟悉,哪怕是刚刚开始学习的新人,也会很快接触到这个对象。而document对象不仅仅是一个普通的JavaScript内置对象,它还是一个巨大API的核心对象,这个巨大的API就是DOM(Document Object Model),它将文档的内容呈现在JS面前,并赋予了JS操作文档的能力。
- DOM树体现着HTML页面的层级结构,学习中经常提到的父元素子元素的说法也是建立在树这种数据结构的基础之上的,而DOM文档树则包含文档中所有内容。
1.2-查找节点:子节点
<script>
console.log(ul.children)
</script>
1.3-查找节点:兄弟节点
<script>
let li2 = document.querySelector('#li2')
document.querySelector('.btn').onclick = function () {
console.log(li2.previousElementSibling)
li2.previousElementSibling.style.color = 'red'
console.log(li2.nextElementSibling)
li2.nextElementSibling.style.color = 'red'
}
</script>
1.4-查找节点:父节点
<script>
let li2 = document.querySelector('#li2')
console.log(li2.parentNode)
console.log(li2.parentNode.parentNode)
console.log(li2.parentNode.parentNode.parentNode)
console.log(li2.parentNode.parentNode.parentNode.parentNode)
console.log(li2.parentNode.parentNode.parentNode.parentNode.parentNode)
</script>
1.5-点击关闭二维码按钮
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>关闭二维码</title>
<style>
.box {
border: 1px solid #D9D9D9;
margin: 100px auto;
position: relative;
width: 107px;
}
#x {
border: 1px solid #D9D9D9;
width: 14px;
height: 14px;
line-height: 14px;
color: #D6D6D6;
cursor: pointer;
position: absolute;
top: 0;
left: -15px;
}
</style>
</head>
<body>
<div class="box">
<img src="images/taobao.jpg" alt="" />
<span id="x">×</span>
</div>
<script>
let x = document.querySelector('#x')
x.onclick = function(){
this.parentNode.style.display = 'none'
}
</script>
</body>
</html>
1.6-新增节点
<body>
<ul>
<li>我是班长1</li>
<li id="li2">我是班长2</li>
<li>我是班长3</li>
<li>我是班长4</li>
<li>我是班长5</li>
</ul>
<script>
let ul = document.querySelector('ul')
let newLi = document.createElement('li')
newLi.innerText = '我是新来的'
console.log(newLi)
ul.appendChild(newLi)
</script>
1.7-克隆节点
<!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>Document</title>
<style>
.box {
width: 300px;
height: 100px;
border: 1px solid pink;
margin-top: 10px;
}
</style>
</head>
<body>
<button class="btn">点我克隆节点</button>
<button class="btn1">点我克隆节点</button>
<div class="box">
<a href="我是链接"></a>
<p>我是p标签</p>
<ul>
<li>我是ikun1</li>
<li>我是ikun2</li>
</ul>
</div>
<script>
let btn = document.querySelector('.btn')
btn.onclick = function () {
let cBox = document.querySelector('.box').cloneNode(true)
document.body.appendChild(cBox)
}
let btn1 = document.querySelector('.btn1')
btn1.onclick = function () {
let cBox = document.querySelector('.box').cloneNode(false)
document.body.appendChild(cBox)
}
</script>
</body>
</html>
1.8-删除节点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button class="btn">点我删除节点</button>
<ul>
<li>我是班长1</li>
<li id="li2">我是班长2</li>
<li>我是班长3</li>
<li>我是班长4</li>
<li>我是班长5</li>
</ul>
<script>
document.querySelector('.btn').onclick = function(){
let ul = document.querySelector('ul')
let li2 = document.querySelector('#li2')
ul.removeChild( li2 )
}
</script>
</body>
</html>
1.9-insertBefore实现上移动与下移
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button class="btn1">新增元素到最后面</button>
<button class="btn2">新增元素到li2前面</button>
<button class="btn3">新增元素到最前面</button>
<button class="btn4">li2上移</button>
<button class="btn5">li2下移</button>
<ul>
<li>我是班长1</li>
<li id="li2">我是班长2</li>
<li>我是班长3</li>
<li>我是班长4</li>
<li>我是班长5</li>
</ul>
<script>
let ul = document.querySelector('ul')
let li2 = document.querySelector('#li2')
document.querySelector('.btn1').onclick = function(){
let newLi = document.createElement('li')
newLi.innerText = '我是新来的1'
ul.appendChild(newLi)
}
document.querySelector('.btn2').onclick = function(){
let newLi = document.createElement('li')
newLi.innerText = '我是新来的2'
ul.insertBefore( newLi,li2 )
}
document.querySelector('.btn3').onclick = function(){
let newLi = document.createElement('li')
newLi.innerText = '我是新来的3'
ul.insertBefore( newLi, ul.children[0] )
}
document.querySelector('.btn4').onclick = function(){
if( li2.previousElementSibling ){
ul.insertBefore( li2, li2.previousElementSibling )
}else{
alert('已经是第一个元素')
}
}
document.querySelector('.btn5').onclick = function(){
if( li2.nextElementSibling ){
ul.insertBefore(li2, li2.nextElementSibling.nextElementSibling )
}else{
alert('已经是最后一个元素')
}
}
</script>
</body>
</html>
02-节点操作语法总结
<script>
</script>
03.案例分享
1.案例:微博发布
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>微博发布</title>
<style>
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
.w {
width: 900px;
margin: 0 auto;
}
.controls textarea {
width: 878px;
height: 100px;
resize: none;
border-radius: 10px;
outline: none;
padding-left: 20px;
padding-top: 10px;
font-size: 18px;
}
.controls {
overflow: hidden;
}
.controls div {
float: right;
}
.controls div span {
color: #666;
}
.controls div .useCount {
color: red;
}
.controls div button {
width: 100px;
outline: none;
border: none;
background: rgb(0, 132, 255);
height: 30px;
cursor: pointer;
color: #fff;
font: bold 14px '宋体';
transition: all 0.5s;
}
.controls div button:hover {
background: rgb(0, 225, 255);
}
.controls div button:disabled {
background: rgba(0, 225, 255, 0.5);
}
.contentList {
margin-top: 50px;
}
.contentList li {
padding: 20px 0;
border-bottom: 1px dashed #ccc;
position: relative;
}
.contentList li .info {
position: relative;
}
.contentList li .info span {
position: absolute;
top: 15px;
left: 100px;
font: bold 16px '宋体';
}
.contentList li .info p {
position: absolute;
top: 40px;
left: 100px;
color: #aaa;
font-size: 12px;
}
.contentList img {
width: 80px;
border-radius: 50%;
}
.contentList li .content {
padding-left: 100px;
color: #666;
word-break: break-all;
}
.contentList li .the_del {
position: absolute;
right: 0;
top: 0;
font-size: 28px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="w">
<!-- 操作的界面 -->
<div class="controls">
<img src="./images/9.6/tip.png" alt="" /><br />
<!-- maxlength 可以用来限制表单输入的内容长度 -->
<textarea
placeholder="说点什么吧..."
id="area"
cols="30"
rows="10"
maxlength="200"
></textarea>
<div>
<span class="useCount" id="useCount">0</span>
<span>/</span>
<span>200</span>
<button id="send">发布</button>
</div>
</div>
<!-- 微博内容列表 -->
<div class="contentList">
<ul id="list"></ul>
</div>
</div>
<!-- 添加了hidden属性元素会直接隐藏掉 -->
<li hidden>
<div class="info">
<img class="userpic" src="./images/9.6/03.jpg" />
<span class="username">死数据:百里守约</span>
<p class="send-time">死数据:发布于 2020年12月05日 00:07:54</p>
</div>
<div class="content">死数据:111</div>
<span class="the_del">X</span>
</li>
<script>
let dataArr = [
{ uname: '司马懿', imgSrc: './images/9.5/01.jpg' },
{ uname: '女娲', imgSrc: './images/9.5/02.jpg' },
{ uname: '百里守约', imgSrc: './images/9.5/03.jpg' },
{ uname: '亚瑟', imgSrc: './images/9.5/04.jpg' },
{ uname: '虞姬', imgSrc: './images/9.5/05.jpg' },
{ uname: '张良', imgSrc: './images/9.5/06.jpg' },
{ uname: '安其拉', imgSrc: './images/9.5/07.jpg' },
{ uname: '李白', imgSrc: './images/9.5/08.jpg' },
{ uname: '阿珂', imgSrc: './images/9.5/09.jpg' },
{ uname: '墨子', imgSrc: './images/9.5/10.jpg' },
{ uname: '鲁班', imgSrc: './images/9.5/11.jpg' },
{ uname: '嬴政', imgSrc: './images/9.5/12.jpg' },
{ uname: '孙膑', imgSrc: './images/9.5/13.jpg' },
{ uname: '周瑜', imgSrc: './images/9.5/14.jpg' },
{ uname: '老夫子', imgSrc: './images/9.5/15.jpg' },
{ uname: '狄仁杰', imgSrc: './images/9.5/16.jpg' },
{ uname: '扁鹊', imgSrc: './images/9.5/17.jpg' },
{ uname: '马可波罗', imgSrc: './images/9.5/18.jpg' },
{ uname: '露娜', imgSrc: './images/9.5/19.jpg' },
{ uname: '孙悟空', imgSrc: './images/9.5/20.jpg' },
{ uname: '黄忠', imgSrc: './images/9.5/21.jpg' },
{ uname: '百里玄策', imgSrc: './images/9.5/22.jpg' }
]
let area = document.querySelector('#area')
let useCount = document.querySelector('#useCount')
let send = document.querySelector('#send')
let list = document.querySelector('#list')
area.oninput = function(){
useCount.innerText = this.value.length
}
send.onclick = function(){
if( area.value.trim() == '' ){
alert('请输入内容')
}else{
let li = document.createElement('li')
let index = parseInt( Math.random()*dataArr.length )
li.innerHTML = `<div class="info">
<img class="userpic" src="${ dataArr[index].imgSrc }" />
<span class="username">${ dataArr[index].uname }</span>
<p class="send-time">发布于 ${ new Date().toLocaleString() }</p>
</div>
<div class="content">${ area.value }</div>
<span class="the_del">X</span>`
list.insertBefore( li , list.children[0] )
li.querySelector('.the_del').onclick = function(){
list.removeChild( this.parentNode )
}
}
area.value = ''
useCount.innerText = 0
}
</script>
</body>
</html>
04.定时器
1.定时器 : 一断代码 间隔时间 重复执行
2.定时器语法 :
2.1 setInterval() :
永久定时器。 一旦开启永久重复执行,只能手动清除 开启: let timeID = setInterval( function(){}, 间隔时间 ) 清除: clearInterval( timeID )
2.2 setTimeout() :
一次定时器。 间隔时间只会执行一次,之后自动清除 开启: let timeID = setTimeout( function(){}, 间隔时间 ) 清除: clearTimeout( timeID )
/**
* @description: 开启定时器
* @param {function} 一段代码
* 回调函数 : 如果一个函数的参数是函数,这个参数函数叫做回调函数
* @param {number} 间隔时间, 单位毫秒ms 1s = 1000ms
* @return: 定时器id
* 一个页面可以开启很多个定时器,浏览器为了便于管理。就会给每一个定时器一个编号,称之为定时器id
*/
01. 3秒后自动关闭广告 setTimeout() : 一次定时器
<style>
img {
position: absolute;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<img src="./images/ad.png" alt="" />
<script>
setTimeout( function(){
document.querySelector('img').style.display = 'none'
} , 3000 )
</script>
02.日期对象Date()
<script>
let d = new Date()
console.log( d )
console.log( d.toLocaleString() )
console.log( d.toLocaleDateString() )
console.log( d.toLocaleTimeString() )
console.log( d.getFullYear() )
console.log( d.getMonth() )
console.log( d.getDate() )
console.log( d.getDay() )
console.log( d.getHours() )
console.log( d.getMinutes() )
console.log( d.getSeconds() )
console.log( +d )
console.log( Date.now() )
</script>
03.案例:电商秒杀(setInterval永久定时器)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
span{
display: inline-block;
width: 50px;
height: 50px;
background-color: #000;
color: #fff;
text-align: center;
line-height: 50px;
font-size: 25px;
font-weight: 700;
}
</style>
</head>
<body>
<div>
<span id="hour">00</span> : <span id="minute">00</span> :
<span id="second">15</span>
</div>
<script>
let timeID = setInterval( function(){
let h = +document.querySelector('#hour').innerText
let m = +document.querySelector('#minute').innerText
let s = +document.querySelector('#second').innerText
s--
if(s < 0 ){
s = 59
m--
}
if(m < 0 ){
m = 59
h--
}
h = h < 10 ? '0' + h : h
m = m < 10 ? '0' + m : m
s = s < 10 ? '0' + s : s
document.querySelector('#hour').innerText = h
document.querySelector('#minute').innerText = m
document.querySelector('#second').innerText = s
if( h == 0 && m == 0 && s == 0){
clearInterval(timeID)
}
},1000)
</script>
</body>
</html>
04.案例:协议禁用按钮
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
span{
display: inline-block;
width: 50px;
height: 50px;
background-color: #000;
color: #fff;
text-align: center;
line-height: 50px;
font-size: 25px;
font-weight: 700;
}
</style>
</head>
<body>
<div>
<span id="hour">00</span> : <span id="minute">00</span> :
<span id="second">15</span>
</div>
<script>
let timeID = setInterval( function(){
let h = +document.querySelector('#hour').innerText
let m = +document.querySelector('#minute').innerText
let s = +document.querySelector('#second').innerText
s--
if(s < 0 ){
s = 59
m--
}
if(m < 0 ){
m = 59
h--
}
h = h < 10 ? '0' + h : h
m = m < 10 ? '0' + m : m
s = s < 10 ? '0' + s : s
document.querySelector('#hour').innerText = h
document.querySelector('#minute').innerText = m
document.querySelector('#second').innerText = s
if( h == 0 && m == 0 && s == 0){
clearInterval(timeID)
}
},1000)
</script>
</body>
</html>
04-拓展进阶:关于渲染引擎的重绘与回流介绍
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ABcpb77F-1639577454388)(day03.assets/1636883841422.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-62HPJUfP-1639577454390)(day03.assets/1636966604129.png)]
|