最後的錯誤是什麼?謝謝。爲什麼在這個簡單的addEventListener函數後使用'false'?
window.addEventListener('load', function() {
alert("All done");
}, false);
最後的錯誤是什麼?謝謝。爲什麼在這個簡單的addEventListener函數後使用'false'?
window.addEventListener('load', function() {
alert("All done");
}, false);
據MDC,第三個參數是:
將useCapture
如果true
,useCapture
表明用戶希望 開始捕獲了。啓動 捕獲後,指定 類型的所有活動將在DOM樹是 分派到任何EventTarget
小號 下方之前被分派到 註冊listener
。 通過樹向上冒泡的事件將 不觸發指定爲 的偵聽器使用捕獲。有關詳細說明,請參閱DOM Level 3 Events 。
它確定事件是否被捕獲。
更多信息here
我檢查MDN太多,但我還是不明白什麼useCapture
是對,所以這個答案是爲那些誰還不在檢查的正式文件後得到它。
所以首先,以下在發生幾乎所有的browers:
在所有的瀏覽器,除了IE < 9,有事件處理的兩個階段。
這項活動首次下降 - 這就是所謂的捕捉,然後泡了。這種行爲在W3C規範中是標準化的。
這意味着無論您將useCapture
設置爲,這兩個事件階段總是存在。
此圖顯示了它的工作原理。
根據這個模型中,事件:
捕獲向下 - 通過1 - > 2 - > 3.
氣泡向上 - 通過3 - > 2 - > 1
然後出現你的問題。名爲useCapture
的第三個參數表示您希望處理程序處理事件的兩個階段中的哪一個。
useCapture = true
處理程序設定在捕獲階段。活動會在得到它的孩子之前得到它。
useCapture = false
。該處理程序設置在冒泡階段。活動將在得到其子女後得到。
這意味着,如果你寫這樣的代碼:
child.addEventListener("click", second);
parent.addEventListener("click", first, true);
點擊子元素時
,first
方法將被second
之前調用。
默認情況下,useCapture
標誌設置爲假這意味着你處理只會事件冒泡階段時被調用。
非常好,全面的答案。我比現在更瞭解這一點。 – 0x499602D2 2014-08-31 14:19:24
非常好的解釋。人情味,這是什麼使差異。 – 2015-04-28 00:54:56
我很欣賞這個解釋。 – neilsimp1 2016-04-08 13:21:59
@ Libra的答案非常好,只是碰巧有些像我這樣的人更好地理解代碼與機器的交互。
所以下面的腳本應該解釋事件傳播。 我試圖做這個基礎是description schema:
以下事件流下來,上了以下層次:
<window>
<document>
<body>
<section>
<div>
<paragraph>
<span>
爲了簡單起見,我們將開始在身體下到span元素註冊捕獲階段的處理程序,並返回到冒泡階段註冊處理程序的主體元素。 所以結果將是節點從節點開始到結束所採取的方向。 請點擊片段的右側面板上的「顯示控制檯」訪問日誌
function handler(e){
/* logs messages of each phase of the event flow associated with
the actual node where the flow was passing by */
if (e.eventPhase == Event.CAPTURING_PHASE){
console.log ("capturing phase :\n actual node : "+this.nodeName);
}
if (e.eventPhase == Event.AT_TARGET){
console.log("at target phase :\n actual node : "+this.nodeName);
}
if (e.eventPhase == Event.BUBBLING_PHASE){
console.log ("bubbling phase :\n actual node : "+this.nodeName);
}
}
/* The following array contains the elements between the target (span and you can
click also on the paragraph) and its ancestors up to the BODY element, it can still
go up to the "document" then the "window" element, for the sake of simplicity it is
chosen to stop here at the body element
*/
arr=[document.body,document.getElementById("sectionID"),
document.getElementById("DivId"),document.getElementById("PId"),
document.getElementById("spanId")];
/* Then trying to register handelers for both capturing and bubbling phases
*/
function listener(node){
node.addEventListener(ev, handler, bool)
/* ev :event (click), handler : logging the event associated with
the target, bool: capturing/bubbling phase */
}
ev="click";
bool=true; // Capturing phase
arr.forEach(listener);
bool=false; // Bubbling phase
/* Notice that both capturing and bubbling
include the at_target phase, that's why you'll get two `at_target` phases in
the log */
arr.forEach(listener);
p {
background: gray;
color:white;
padding: 10px;
margin: 5px;
border: thin solid black
}
span {
background: white;
color: black;
padding: 2px;
cursor: default;
}
<section ID="sectionID">
<div id="DivId">
<p id="PId">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis <span id="spanId">CLICK ME </span> imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosq.
</p>
</div>
</section>
注意的事件,如重點不冒泡,這使得SENS他仍然多數事件做泡沫。
我不知道這麼多關於JavaScript,所以我有麻煩得到這個答案。我其實不知道什麼是useCapture?請你告訴我一些關於它的事情。 – 2014-01-10 06:25:58
@BikashChandraMondal查看下面的答案。 – libra 2014-11-20 03:21:17
請解釋一下,不要只是複製/粘貼。 – 2016-07-26 22:58:53