一、事件
- 什么是事件?
- 事件是在编程时系统内发生的动作或者发生的事情
- 比如用户在网页上单击一个按钮
- 什么是事件监听?
- 就是让程序检测是否有事件产生,一旦有事件触发,就立即调用一个函数做出响应,也称为 注册事件
- 语法:元素.addEventListener('事件', 要执行的函数)
- 事件监听三要素:
- 事件源: 那个dom元素被事件触发了,要获取dom元素
- 事件: 用什么方式触发,比如鼠标单击 click、鼠标经过 mouseover 等
- 事件调用的函数: 要做什么事
01-添加事件监听
<body>
<button>点我啊</button>
<p>点击有事发生</p>
<script>
let btn = document.querySelector('button')
btn.addEventListener('click', function(){
console.log(123);
})
</script>
</body>
02-点击关闭二维码
<!DOCTYPE html>
<html lang="zh-CN">
<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>03-事件案例-点击关闭二维码</title>
<style>
.erweima {
position: relative;
width: 160px;
height: 160px;
margin: 100px auto;
}
span {
position: absolute;
left: -20px;
top: 0;
display: block;
width: 20px;
text-align: center;
border: 1px solid #ccc;
cursor: pointer;
}
</style>
</head>
<body>
<div class="erweima">
<img src="./images/code.png" alt="" />
<span> X </span>
</div>
<script>
let span = document.querySelector('span');
let erweima = document.querySelector('.erweima');
span.addEventListener('click', function(){
erweima.style.display = 'none';
})
</script>
</body>
</html>
03-随机点名
<body>
<p>这里显示名字</p>
<button>随机点名</button>
<script>
let names = [
'彭泉林',
'徐祥庭',
'温晓桢',
'黄素蘭',
'徐斌',
'陈志浩',
'王刘杰'
]
let btn = document.querySelector('button');
let p = document.querySelector('p');
btn.addEventListener('click',function(){
let index = parseInt(Math.random() * names.length);
p.innerHTML = names[index];
names.splice(index,1);
if(names.length == 0){
btn.disabled = true;
}
})
</script>
</body>
04-随机点名-进阶版
<body>
<p>这里显示名字</p>
<button class="start">开始随机点名</button>
<button class="end">结束随机点名</button>
<script>
let names = [
'彭泉林',
'徐祥庭',
'温晓桢',
'黄素蘭',
'徐斌',
'陈志浩',
'王刘杰'
]
let p = document.querySelector('p');
let start = document.querySelector('.start');
let end = document.querySelector('.end');
let timeId,index;
start.addEventListener('click',function(){
timeId = setInterval(function(){
index = parseInt(Math.random() * names.length);
p.innerHTML = names[index];
},100)
})
end.addEventListener('click',function(){
clearInterval(timeId);
names.splice(index,1);
if(names.length == 0){
start.disabled = true
end.disabled = true
}
})
</script>
</body>
05-事件的其它的绑定方式
<body>
<button>点我啊</button>
<script>
let btn = document.querySelector('button')
btn.onclick = function() {
console.log(123)
}
btn.onclick = function() {
console.log(234)
}
</script>
</body>
06-常见的事件类型
<body>
<button>进入后弹出层</button>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<br />
用户名:<input type="text" class="username" /> <br />
密码:<input type="text" class="password" /> <br />
<script>
let button = document.querySelector('button')
let ul = document.querySelector('ul')
let username = document.querySelector('.username')
username.addEventListener('keydown', function() {
console.log('abc')
})
username.addEventListener('keyup', function() {
console.log('1234')
})
username.addEventListener('focus', function() {
})
username.addEventListener('blur', function() {
})
button.addEventListener('mouseenter', function() {
ul.style.display = 'block'
})
button.addEventListener('mouseleave', function() {
ul.style.display = 'none'
})
</script>
</body>
07-自动切换图片-鼠标进入和离开事件的应用
<!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>
* {
padding: 0;
margin: 0;
}
div {
width: 700px;
height: 320px;
margin: 100px auto;
position: relative;
}
p {
position: absolute;
left: 0;
bottom: 0;
line-height: 50px;
background-color: #666;
color: #fff;
width: 100%;
padding-left: 10px;
font-size: 20px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div>
<img src="./images/b01.jpg" alt="" />
<p>第1张图片</p>
</div>
<script>
let img = document.querySelector('img')
let p = document.querySelector('p')
let div = document.querySelector('div')
let index = 1
let timeId = setInterval(function() {
index++
if (index > 9) {
index = 1
}
img.src = `./images/b0${index}.jpg`
p.innerHTML = `第${index}张图片`
}, 1000)
div.addEventListener('mouseenter', function() {
clearInterval(timeId)
})
div.addEventListener('mouseleave', function() {
timeId = setInterval(function() {
index++
if (index > 9) {
index = 1
}
img.src = `./images/b0${index}.jpg`
p.innerHTML = `第${index}张图片`
}, 1000)
})
</script>
</body>
</html>
08-其它事件类型
<body>
手机号:<input type="text" /> <span>手机号输入不合法</span> <br />
<input type="file" />
<script>
let phone = document.querySelector('input:nth-of-type(1)')
let myfile = document.querySelector('input:nth-of-type(2)')
myfile.addEventListener('change', function() {
console.log('文件选好了,准备上传')
})
</script>
</body>
09-小米搜索框
<!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>09-小米搜索框</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul {
list-style: none;
display: none;
}
.mi {
position: relative;
width: 223px;
margin: 100px auto;
}
.mi input {
width: 223px;
height: 48px;
padding: 0 10px;
font-size: 14px;
line-height: 48px;
border: 1px solid #e0e0e0;
outline: none;
}
.mi .search {
border: 1px solid #ff6700;
}
.result-list {
position: absolute;
left: 0;
top: 48px;
width: 223px;
border: 1px solid #ff6700;
border-top: 0;
background: #fff;
}
.result-list a {
display: block;
padding: 6px 15px;
font-size: 12px;
color: #424242;
text-decoration: none;
}
.result-list a:hover {
background-color: #eee;
}
</style>
</head>
<body>
<div class="mi">
<input type="search" placeholder="小米笔记本" />
<ul class="result-list">
<li><a href="#">全部商品</a></li>
<li><a href="#">小米11</a></li>
<li><a href="#">小米10S</a></li>
<li><a href="#">小米笔记本</a></li>
<li><a href="#">小米手机</a></li>
<li><a href="#">黑鲨4</a></li>
<li><a href="#">空调</a></li>
</ul>
</div>
<script>
let input = document.querySelector('input')
let ul = document.querySelector('ul')
input.addEventListener('focus', function() {
ul.style.display = 'block'
})
input.addEventListener('blur', function() {
ul.style.display = 'none'
})
</script>
</body>
</html>
10-微博模板
* {
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;
}
.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;
}
<body>
<div class="w">
<div class="controls">
<img src="images/tip.png" alt="" /><br />
<textarea
placeholder="说点什么吧..."
id="area"
cols="30"
rows="10"
maxlength="200"
></textarea>
<div>
<span class="useCount">0</span>
<span>/</span>
<span>200</span>
<button id="send">发布</button>
</div>
</div>
<div class="contentList">
<ul></ul>
</div>
</div>
<script>
let area = document.querySelector('#area')
let useCount = document.querySelector('.useCount')
area.addEventListener('input', function() {
let content = area.value
let count = content.length
useCount.innerHTML = count
})
</script>
</body>
11-全选反选案例(一)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>10-全选反选案例</title>
<style>
* {
margin: 0;
padding: 0;
}
table {
border-collapse: collapse;
border-spacing: 0;
border: 1px solid #c0c0c0;
width: 500px;
margin: 100px auto;
text-align: center;
}
th {
background-color: #09c;
font: bold 16px '微软雅黑';
color: #fff;
height: 24px;
}
td {
border: 1px solid #d0d0d0;
color: #404060;
padding: 10px;
}
.allCheck {
width: 80px;
}
</style>
</head>
<body>
<table>
<tr>
<th class="allCheck">
<input type="checkbox" name="" id="checkAll" />
<span class="all">全选</span>
</th>
<th>商品</th>
<th>商家</th>
<th>价格</th>
</tr>
<tr>
<td>
<input type="checkbox" name="check" class="ck" />
</td>
<td>小米手机</td>
<td>小米</td>
<td>¥1999</td>
</tr>
<tr>
<td>
<input type="checkbox" name="check" class="ck" />
</td>
<td>小米净水器</td>
<td>小米</td>
<td>¥4999</td>
</tr>
<tr>
<td>
<input type="checkbox" name="check" class="ck" />
</td>
<td>小米电视</td>
<td>小米</td>
<td>¥5999</td>
</tr>
</table>
<script>
let chkAll = document.querySelector('#checkAll')
let chks = document.querySelectorAll('.ck')
let all = document.querySelector('.all')
chkAll.addEventListener('click', function() {
let state = chkAll.checked
chks.forEach(function(ele, index) {
ele.checked = state
})
all.innerHTML = state ? '取消' : '全选'
})
chks.forEach(function(ele, index) {
ele.addEventListener('click', function() {
chkAll.checked = true
chks.forEach(function(ele) {
if (ele.checked == false) {
chkAll.checked = false
}
})
all.innerHTML = chkAll.checked ? '取消' : '全选'
})
})
</script>
</body>
</html>
12-全选反选案例-基于数量进行比较
<script>
let chkAll = document.querySelector('#checkAll')
let chks = document.querySelectorAll('.ck')
let all = document.querySelector('.all')
chkAll.addEventListener('click', function() {
let state = chkAll.checked
chks.forEach(function(ele, index) {
ele.checked = state
})
all.innerHTML = state ? '取消' : '全选'
})
chks.forEach(function(ele, index) {
ele.addEventListener('click', function() {
let cnt = 0
chks.forEach(function(ele) {
if (ele.checked == true) {
cnt++
}
})
chkAll.checked = cnt == chks.length
all.innerHTML = chkAll.checked ? '取消' : '全选'
})
})
</script>
13-全选反选案例-基于数量进行比较简化版
<script>
let chkAll = document.querySelector('#checkAll')
let chks = document.querySelectorAll('.ck')
let all = document.querySelector('.all')
chkAll.addEventListener('click', function() {
let state = chkAll.checked
chks.forEach(function(ele, index) {
ele.checked = state
})
all.innerHTML = state ? '取消' : '全选'
})
chks.forEach(function(ele, index) {
ele.addEventListener('click', function() {
let state =
document.querySelectorAll('.ck:checked').length == chks.length
chkAll.checked = state
all.innerHTML = state ? '取消' : '全选'
})
})
</script>
14-购物车加减操作
<body>
<div>
<input type="text" id="total" value="4" readonly />
<input type="button" value="+" id="add" />
<input type="button" value="-" id="reduce" />
</div>
<script>
let add = document.querySelector('#add')
let reduce = document.querySelector('#reduce')
let total = document.querySelector('#total')
add.addEventListener('click', function() {
let v = total.value
v++
total.value = v
reduce.disabled = false
})
reduce.addEventListener('click', function() {
let v = total.value
v--
total.value = v
if (v == 1) {
reduce.disabled = true
}
})
</script>
</body>
15-高阶函数
- 高阶函数可以被简单理解为函数的高级应用,JavaScript 中函数可以被当成【值】来对待,基于这个特性实现函数的高级应用。
- 【值】就是 JavaScript 中的数据,如数值、字符串、布尔、对象等。
let counter = function (x, y) {
return x + y
}
let result = counter(5, 10)
console.log(result)
- 如果将函数 A 做为参数传递给函数 B 时,我们称函数 A 为回调函数
- 简单理解: 当一个函数当做参数来传递给另外一个函数的时候,这个函数就是回调函数
function fn() {
console.log('我是回调函数')
}
setInterval(fn, 1000)
box.addEventListener('click', function() {
console.log('我也是回调函数')
})
16-this的使用介绍
- 环境对象指的是函数内部特殊的变量 this ,它代表着当前函数运行时所处的环境
- 作用:弄清楚this的指向,可以让我们代码更简洁
- 函数的调用方式不同,this 指代的对象也不同
- 【谁调用, this 就是谁】 是判断 this 指向的粗略规则
- 直接调用函数,其实相当于是 window.函数,所以 this 指代 window
<body>
<div>
<button>点名了1</button>
<button>点名了2</button>
<button>点名了3</button>
</div>
<script>
let btns = document.querySelectorAll('button')
btns.forEach(function(ele) {
ele.addEventListener('click', function() {
console.log(this)
this.style.color = 'red'
})
})
</script>
</body>
17-排他思想
<style>
ul {
list-style: none;
width: 600px;
display: flex;
height: 50px;
}
li {
flex: 1;
background-color: #ccc;
border-right: 1px solid blue;
line-height: 50px;
text-align: center;
}
.active {
background-color: orange;
}
</style>
</head>
<body>
<ul class="list">
<li class="active">首页</li>
<li>文章列表</li>
<li>发表文章</li>
<li>关于我们</li>
</ul>
<script>
let lis = document.querySelectorAll('li')
lis.forEach(function(ele) {
ele.addEventListener('click', function() {
lis.forEach(function(subele) {
subele.classList.remove('active')
})
ele.classList.add('active')
})
})
</script>
</body>
</html>
18-排他思想-优化
<script>
let lis = document.querySelectorAll('li')
lis.forEach(function(ele) {
ele.addEventListener('click', function() {
document.querySelector('.list > li.active').classList.remove('active')
ele.classList.add('active')
})
})
</script>
19-tab栏案例
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
.wrapper {
width: 1000px;
height: 475px;
margin: 0 auto;
margin-top: 100px;
}
.tab {
border: 1px solid #ddd;
border-bottom: 0;
height: 36px;
width: 320px;
}
.tab li {
position: relative;
float: left;
width: 80px;
height: 34px;
line-height: 34px;
text-align: center;
cursor: pointer;
border-top: 4px solid #fff;
}
.tab span {
position: absolute;
right: 0;
top: 10px;
background: #ddd;
width: 1px;
height: 14px;
overflow: hidden;
}
.products {
width: 1002px;
border: 1px solid #ddd;
height: 476px;
}
.products .main {
float: left;
display: none;
}
.products .main.active {
display: block;
}
.tab li.active {
border-color: red;
border-bottom: 0;
}
</style>
</head>
<body>
<div class="wrapper">
<ul class="tab">
<li class="tab-item active">国际大牌<span>◆</span></li>
<li class="tab-item">国妆名牌<span>◆</span></li>
<li class="tab-item">清洁用品<span>◆</span></li>
<li class="tab-item">男士精品</li>
</ul>
<div class="products">
<div class="main active">
<a href="###"><img src="imgs/guojidapai.jpg" alt=""/></a>
</div>
<div class="main">
<a href="###"><img src="imgs/guozhuangmingpin.jpg" alt=""/></a>
</div>
<div class="main">
<a href="###"><img src="imgs/qingjieyongpin.jpg" alt=""/></a>
</div>
<div class="main">
<a href="###"><img src="imgs/nanshijingpin.jpg" alt=""/></a>
</div>
</div>
</div>
<script>
let lis = document.querySelectorAll('.wrapper li')
let mains = document.querySelectorAll('.products .main')
lis.forEach(function(ele,index) {
ele.addEventListener('click', function() {
document.querySelector('.wrapper li.active').classList.remove('active')
ele.classList.add('active')
console.log(index);
document.querySelector('.products .main.active').classList.remove('active')
mains[index].classList.add('active')
})
})
</script>
</body>
</html>