<!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>複製代碼