所谓事件,就是当你在执行某个动作会引发其他的函数
为一个元素添加事件有很多方法,一是直接在HTML里内联(不推荐,不方便维护),二是事件处理属性,如下
1.焦点事件.onfocus
,解除焦点状态.onblur
2.点击事件.onclick
3.双击时间.ondbclick
4.按下并松开事件.onkeypress
,按下.onkeydown
,松开onkeyup
,只能是window对象
5.鼠标滑入事件.onmouseover
,鼠标移出事件.onmouseout
注释: 将您的编程逻辑与内容分离也会使您的站点对搜索引擎更加友好。
三是使用addEventLisener(event type,linsener)
,第一个参数是事件类型,第二个是函数,可以使匿名函数,但注意,这种方法不兼容IE8及以下
事件对象
执行函数的对象,通常作为参数传递给函数
示例: 下面是有16个格子,点击会随机变换颜色
source.html >folded1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Useful event target example</title> <style> div { background-color: red; height: 100px; width: 25%; float: left; } </style> </head> <body> <script> for(let i = 1; i <= 16; i++) { const myDiv = document.createElement('div'); document.body.appendChild(myDiv); }
function random(number) { return Math.floor(Math.random() * number); }
function bgChange() { const rndCol = 'rgb(' + random(255) + ',' + random(255) + ',' + random(255) + ')'; return rndCol; }
const divs = document.querySelectorAll('div');
for(let i = 0; i < divs.length; i++) { divs[i].onclick = function(e) { e.target.style.backgroundColor = bgChange(); } } </script> </body> </html>
|
阻止默认行为
希望事件不执行它的行为,比如表单提交,当用户填写不正确时阻止他提交
source.html >folded1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <body> <form> <div> <label for="fname">First name: </label> <input id="fname" type="text"> </div> <div> <label for="lname">Last name: </label> <input id="lname" type="text"> </div> <div> <input id="submit" type="submit"> </div> </form> <p id='myp'></p> <script> var form = document.querySelector('form'); var fname = document.getElementById('fname'); var lname = document.getElementById('lname'); var submit = document.getElementById('submit'); var para = document.querySelector('#myp'); form.onsubmit = function(e) { if (fname.value === '' || lname.value === '') { e.preventDefault(); para.textContent = 'You need to fill in both names!'; } } </script> </body>
|
事件冒泡与捕捉
在实际运用事件会碰到一个问题,父元素和子元素都有事件时如何处理
微软的处理模型是时事件冒泡,也就是先处理子元素再处理父元素,网景(Netscape)是事件捕获,也就是先处理父元素再处理子元素
举个例子
outer-noneCaptrue
middle-noneCaptrue
outer-Captrue
middle-Captrue
source.html >folded1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| <!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> .outer, .middle { display: block; width: 80%; padding: 15px; margin: 15px; text-decoration: none; } .outer { border: 1px solid red; color: red; } .middle { border: 1px solid green; color: green; width: 80%; } </style> </head> <body> <div class="outer"> outer-noneCaptrue <div class="middle" target="_blank"> middle-noneCaptrue </div> </div> <div class="outer"> outer-Captrue <div class="middle" target="_blank"> middle-Captrue </div> </div> <script> const outer = document.getElementsByClassName('outer' )[0]; const middle = document.getElementsByClassName('middle')[0]; const outer2 = document.getElementsByClassName('outer' )[1]; const middle2 = document.getElementsByClassName('middle')[1]; const captrue = { captrue : true }; const noneCaptrue = { captrue : false }; outer.addEventListener('click', onceHandler, noneCaptrue); middle.addEventListener('click', captrueHandler, noneCaptrue); outer2.addEventListener('click', onceHandler, captrue); middle2.addEventListener('click', captrueHandler, captrue); function onceHandler(event) { alert('outer'); } function captrueHandler(event) { alert('middle'); } </script> </body> </html>
|
- noneCaptrue也就是事件冒泡模型
- 点击middle,提示middle–>outer
- 点击outer,提示outer
- Captrue就是事件捕获模型
- 点击middle,提示outer–>middle
- 点击outer,提示outer
当然有时候并不需要触发父元素的事件,如果上面示例点击middle就只需要提示middle,改一下函数
1 2 3 4
| function captrueHandler(event) { event.stopPropagtion; alert('middle'); }
|
事件委托
当一个父元素的子元素很多并且需要在每个子元素添加一样的事件时,只需要在它的父元素添加这个事件即可
作业
照片库
source.html >folded1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| <!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{ width: 100%; margin: 0 auto; } .full-img{ position: relative; width: 100%; height: 480px; } .overlay, .dark, .light{ position: absolute; } .overlay{ width: 100%; height: 480px; top: 0; left: 0; } .dark,.light{ top: 2px; left: 2px; background-color: rgba(255, 255, 255, 0.6); } .thumb-bar img{ width: 20%; float: left; display: block; } </style> </head> <body> <h1>照片库</h1> <div class="full-img"> <img class="displayed-img" src="images/1.jpg"> <div class="overlay"></div> <button class="light">变暗</button> </div> <div class="thumb-bar"></div> <script> const thumbar = document.querySelector('.thumb-bar'); const display = document.querySelector('.displayed-img') const overlay = document.querySelector('.overlay'); const btn = document.querySelector('.light'); for(let i=1; i<3; i++){ const myImg = document.createElement('img'); myImg.setAttribute('src','images/'+i+''+'.jpg'); myImg.onclick = function(e){ let src = e.target.getAttribute('src'); console.log('得到图片路径:'+src); srcChange(src); } thumbar.appendChild(myImg); } function srcChange(src){ display.setAttribute('src',src); } btn.onclick = function (){ if(btn.getAttribute('class') !== 'light'){ btn.setAttribute('class','light'); overlay.style.backgroundColor = 'rgba(0,0,0,0)'; } else { btn.setAttribute('class','dark'); overlay.style.backgroundColor = 'rgba(0,0,0,0.6)'; } } </script> </body> </html>
|