<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>13-JavaScript-移入移出事件</title><style>*{margin: 0;padding: 0; }div{width: 300px;height: 300px;background: red; }</style></head><body><div></div><script>let oDiv = document.querySelector("div");// 1.移入事件// oDiv.onmouseover = function () {// console.log("移入事件");// }// 注意点: 对于初学者来讲, 为了不未知的一些BUG, 建议使用onmouseenteroDiv.onmouseenter = function () {console.log("移入事件"); }// 2.移出事件// oDiv.onmouseout = function () {// console.log("移出事件");// }// 注意点: 对于初学者来讲, 为了不未知的一些BUG, 建议使用onmouseleaveoDiv.onmouseleave = function () {console.log("移出事件"); }3.移动事件 oDiv.onmousemove = function () {console.log("移动事件"); }</script></body></html>复制代码
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>17-JavaScript-焦点事件</title></head><body><input type="text"><script>let oInput = document.querySelector("input");// 1.监听input获取焦点oInput.onfocus = function () {console.log("获取到了焦点"); }// 2.监听input失去焦点oInput.onblur = function () {console.log("失去了焦点"); }// 3.监听input内容改变// 注意点: onchange事件只有表单失去焦点的时候, 才能拿到修改以后的数据oInput.onchange = function () {console.log(this.value); }// oninput事件能够时时获取到用户修改以后的数据, 只要用户修改了数据就会调用(执行)// 注意点: oninput事件只有在IE9以上的浏览器才能使用// 在IE9如下, 若是想时时的获取到用户修改以后的数据, 能够经过onpropertychange事件来实现oInput.oninput = function () {console.log(this.value); }</script></body></html>复制代码
注意点:css
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>onclick</title></head><body><button id="btn">我是按钮</button><script>// 这点击事件只会输出 777, 原理就是给对象属性重赋值会对原有的值进行覆盖var oBtn = document.getElementById("btn"); oBtn.onclick = function () { alert("666"); } oBtn.onclick = function () { alert("777"); }</script></body></html>复制代码
注意点:html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>attachEvent</title></head><body><button id="btn">我是按钮</button><script>// 既输出 666 也输出 777,但现代浏览器已经摒弃了这个方法var oBtn = document.getElementById("btn"); oBtn.attachEvent("onclick", function () { alert("666"); }); oBtn.attachEvent("onclick", function () { alert("777"); });</script></body></html>复制代码
注意点:浏览器
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>addEventListener</title></head><body><button id="btn">我是按钮</button><script>// 既输出 666 也输出 777,现代浏览器通用这个方法var oBtn = document.getElementById("btn"); oBtn.addEventListener("click", function () { alert("666"); }); oBtn.addEventListener("click", function () { alert("777"); });</script></body></html>复制代码
看似考虑周全,实则没有必要dom
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>加个判断</title></head><body><button id="btn">我是按钮</button><script>var oBtn = document.getElementById("btn");function addEvent(ele, name, fn) {if(ele.attachEvent){ ele.attachEvent("on"+name, fn); }else{ ele.addEventListener(name, fn); } }</script></body></html>复制代码
注意点:ide
关于阻止默认行为:函数
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>事件对象</title></head><body><button id="btn">我是按钮</button><a href="http://www.baidu.com">百度首页</a><script> var oBtn = document.getElementById("btn"); oBtn.onclick = function (event) { // 兼容性的写法 event = event || window.event; // alert("666"); console.log(event); console.log(typeof event); } let oA = document.querySelector("a"); oA.onclick = function (event) {// 兼容性的写法event = event || window.event; alert("666");// 阻止默认行为return false; // 企业开发推荐// event.preventDefault(); // 高级浏览器// event.returnValue = false; // IE9如下浏览器}</script></body></html>复制代码
注意点:this
为何要么只能是捕获和当前, 要么只能是当前和冒泡?scala
这是JS处理事件的历史问题orm
早期各大浏览器厂商为争夺定义权, 以及对事件的理解不一样, 产生了捕获和冒泡两种流向htm
后续W3C为了兼容, 将两种方式都归入标准
如何设置事件究竟是捕获仍是冒泡?
经过addEventListener方法, 这个方法接收三个参数
注意点:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>41-JavaScript-事件执行的三个阶段</title><style>*{margin: 0;padding: 0; }.father{width: 300px;height: 300px;background: red; }.son{width: 150px;height: 150px;background: blue; }</style></head><body><div class="father"><div class="son"></div></div><script>let oFDiv = document.querySelector(".father");let oSDiv = document.querySelector(".son"); oFDiv.addEventListener("click", function () {console.log("father"); }, false); oSDiv.addEventListener("click", function () {console.log("son"); }, false);</script></body></html><!-- 默认就是 false,也就是冒泡 设置为 true 的元素及其子元素链条都变为捕获,且仅影响自身及其子代,不影响其父元素 -->复制代码
IE6.0: div -> body -> html -> document
其余浏览器: div -> body -> html -> document -> window
注意: 不是全部的事件都能冒泡,有些事件不冒泡,如 blur、 focus、 load、 unload 等
首先要明确:阻止的前提是有事件,阻止事件冒泡的操做是写在实践方法的回调函数里的
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>43-JavaScript-阻止事件冒泡</title><style>*{margin: 0;padding: 0; }.father{width: 300px;height: 300px;background: red; }.son{width: 150px;height: 150px;background: blue; }</style></head><body><div class="father" id="father"><div class="son" id="son"></div></div><script>// 1.拿到须要操做的元素var oFDiv = document.getElementById("father");var oSDiv = document.getElementById("son"); // 2.注册事件监听oFDiv.onclick = function () {console.log("father"); } oSDiv.onclick = function (event) { event = event || window.event;// event.stopPropagation(); // 高级浏览器// event.cancelBubble = true; // 低级浏览器if(event.cancelBubble){ event.cancelBubble = true; }else{ event.stopPropagation(); }console.log("son"); }</script></body></html>复制代码
区别在因而否触发冒泡(或捕获)
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>44-JavaScript-移入移出事件区别</title><style>*{margin: 0;padding: 0; }.father{width: 300px;height: 300px;background: red; }.son{width: 150px;height: 150px;background: blue; }</style></head><body><div class="father"><div class="son"></div></div><script>let oFDiv = document.querySelector(".father");let oSDiv = document.querySelector(".son");/* oFDiv.onmouseover = function () { console.log("father"); } oSDiv.onmouseover = function () { console.log("son"); } *//* oFDiv.onmouseenter = function () { console.log("father"); } oSDiv.onmouseenter = function () { console.log("son"); } */oFDiv.onmouseleave = function () {console.log("father"); } oSDiv.onmouseleave = function () {console.log("son"); }</script></body></html>复制代码
调用事件方法时 return false 能够禁用默认事件, 或者用 event.preventDefault() 也行
若是想获取 query 到的 input 标签对象中输入的内容, 只能经过其 value 属性
html 能够给标签设置自定义标签属性, 能够给 JS 用,
甚至能够经过 JS 给指定 DOM 添加自定义属性标签
在 JS 中若是 HTML 标签的属性名称和取值名称同样的时候, JS 会返回 true/false 但若是是经过代码给 input 设置的数据, 那么不会触发 oninput 事件
对上古浏览器的兼容只需了解便可
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>10-移动端点透问题</title><style> * { margin: 0; padding: 0; } div { text-align: center; font-size: 40px; } .click { width: 300px; height: 300px; background: red; position: absolute; left: 50%; transform: translateX(-50%); top: 100px; } .tap { width: 200px; height: 200px; background: blue; position: absolute; left: 50%; transform: translateX(-50%); top: 150px; } </style></head><body><div class="click">click</div><div class="tap">tap</div><!-- 两元素在同级, 但展示时两个块块有重叠 --><script>let oClick = document.querySelector(".click"); let oTap = document.querySelector(".tap"); oTap.ontouchstart = function (event) { this.style.display = "none"; event.preventDefault(); // 阻止事件扩散 }; oClick.onclick = function () { console.log("click"); }; </script></body></html>复制代码