1 回答

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊
直接在窗口上觸發(fā)的事件窗口沒(méi)有“捕獲階段”?”。
如果您查看事件調(diào)度和 DOM 事件流圖形,您可以看到該流程開(kāi)始于?窗口,因此此類事件將已處于“目標(biāo)階段”狀態(tài)。從第一步開(kāi)始。
這意味著對(duì)于此類事件,capture
?標(biāo)志是無(wú)用的,因?yàn)樗惺录陕?tīng)器都將在“目標(biāo)階段”,因?yàn)樗鼈儠?huì)在您的捕獲處理程序之前觸發(fā)。您無(wú)法停止將事件傳播到先前設(shè)置的處理程序”無(wú)論如何,所以
const phases_dict = {
? [Event.CAPTURING_PHASE]: 'Capturing Phase',
? [Event.AT_TARGET]: 'Target Phase',
? [Event.BUBBLING_PHASE]: 'Bubbling Phase'
};
// First listener defined without capturing flag, will fire first anyway
window.addEventListener( 'blur', evt => {
? const current_phase = phases_dict[ evt.eventPhase ];
? console.log( 'bubble blur is in', current_phase );
}, { capture: false } ); // fire at bubbling phase
// Second listener with capturing flag
window.addEventListener( 'blur', evt => {
? // This one will also capture the events on Elements in the Document
? // For our case, we only want the one on Window
? if( evt.currentTarget === evt.target ) {
? ? const current_phase = phases_dict[ evt.eventPhase ];
? ? console.log( 'capture blur is in', current_phase );
? }
}, { capture: true } ); // fire at capturing phase
// To show that bubbling events work as intended
// We also register click events
window.addEventListener( 'click', evt => {
? const current_phase = phases_dict[ evt.eventPhase ];
? console.log( 'bubble click is in', current_phase );
}, { capture: false } ); // fire at bubbling phase
window.addEventListener( 'click', evt => {
? const current_phase = phases_dict[ evt.eventPhase ];
? console.log( 'capture click is in', current_phase );
}, { capture: true } ); // fire at capturing phase
<pre>Click anywhere in this frame then click somewhere else in SO's page to trigger focus event</pre>
請(qǐng)注意,您可以停止 Elements 的傳播。?模糊事件,但不模糊Window事件。
您必須找到其他方法來(lái)阻止當(dāng)前的偵聽(tīng)器,也許是使用標(biāo)志變量?
- 1 回答
- 0 關(guān)注
- 123 瀏覽
添加回答
舉報(bào)