03JavaScript程序设计修炼之道_2019-06-30_20-42-00_2019-07-02_20-03-55 addEventListener、事件流
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了03JavaScript程序设计修炼之道_2019-06-30_20-42-00_2019-07-02_20-03-55 addEventListener、事件流,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4119字,纯文字阅读大概需要6分钟。
内容图文
19event-listener.html
<!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 id="btn">按钮</button> <script src="../../01-ES/code/tool.js"></script> <script> var btn = document.getElementById("btn"); // 注册事件 /* 第一种 无法给同一个对象注册多个事件处理函数 btn.onclick = function() { alert(123); } btn.onclick = function() { alert(456); // 移除事件 btn.onclick = null; } */ function fn() { alert(123); // 移除事件 //btn.removeEventListener("click",fn); removeEventListener(btn,"click",fn); } btn.addEventListener("click",fn); /* 第二种 兼容Ie9+ btn.addEventListener("click",function() { alert(123); }); btn.addEventListener("click",function() { alert(456); }); //第三种 attachEvent ie6-10 第一个参数 表示事件名称需要加on btn.attachEvent("onclick",function() { alert(456); }); */ // 封装一个函数 对元素注册事件 function addEventListener(ele,eventName,fn) { // 能力检测 if(ele.addEventListener) { ele.addEventListener(eventName,fn); } else if(ele.attachEvent) { ele.attachEvent("on"+eventName,fn); } else { ele["on"+eventName] = fn; } } /*addEventListener(btn,"click",function() { alert(123); }); addEventListener(btn,"click",function() { alert(456); }); */ function fn2() { alert(456); btn.detachEvent("onclick",fn2); } //btn.attachEvent("onclick",fn2); </script> </body> </html>
20event-bubble.html
<!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> div { width: 200px; height: 200px; background-color: pink; } div>p { height: 50px; background: #00f; } </style> </head> <body> <div> <p>123456</p> </div> <ul> <li>北京</li> <li>上海</li> <li>深圳</li> <li>杭州</li> <li>广州</li> </ul> <script> /* var oDiv = document.querySelector("div"); var oP = document.querySelector("p"); // // 第三个参数为false表示冒泡 oDiv.addEventListener("click",function() { console.log("div "); },false); oP.addEventListener("click",function() { console.log("p"); },false); document.addEventListener("click",function() { console.log("document "); },false); window.addEventListener("click",function() { console.log("window "); },false);*/ /* 第三个参数为true表示捕获 oDiv.addEventListener("click",function() { console.log("div2 "); },true); document.addEventListener("click",function() { console.log("document2 "); },true); window.addEventListener("click",function() { console.log("window2 "); },true); */ // 事件分为三个阶段 /* 1 捕获阶段 一般没什么意义 2 执行当前点击的元素 3 冒泡阶段 ** */ /* window window | | document document | | div div | | P */ // 事件委托 原理利用事件冒泡 document.getElementsByTagName("ul")[0].onclick = function(e) { //console.log(e.target);// 真正触发事件的对象 e.target.style.background = "green"; } /* window window | document document | | body body | | ul ul | li */ </script> </body> </html>
21preventDefault.html
<!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> #box1 { width: 200px; height: 200px; background-color: red; } #box2 { width: 100px; height: 100px; background-color: skyblue; } </style> </head> <body> <a href="https://www.baidu.com" id="link">去百度</a> <div id="box1"> <div id="box2"> </div> </div> <script> var link = document.getElementById("link"); link.onclick = function(e) { alert(123); var e = e || event; /* e.preventDefault(); // 取消默认行为 标准dom方法 e.returnValue = false;// IE低版本 */ return false; } var box1 = document.getElementById("box1"); var box2 = document.getElementById("box2"); var arr = [box1,box2]; // onclick注册事件 只有冒泡 attachEvent 只有冒泡 for(var i=0; i<arr.length; i++) { arr[i].onclick = function(e) { console.log(this.id); e.stopPropagation(); // 标准dom e.cancelBubble = true; //ie低版本 } } </script> </body> </html>
内容总结
以上是互联网集市为您收集整理的03JavaScript程序设计修炼之道_2019-06-30_20-42-00_2019-07-02_20-03-55 addEventListener、事件流全部内容,希望文章能够帮你解决03JavaScript程序设计修炼之道_2019-06-30_20-42-00_2019-07-02_20-03-55 addEventListener、事件流所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。