1 回答

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個贊
直接在窗口上觸發(fā)的事件窗口沒有“捕獲階段”?”。
如果您查看事件調(diào)度和 DOM 事件流圖形,您可以看到該流程開始于?窗口,因此此類事件將已處于“目標(biāo)階段”狀態(tài)。從第一步開始。
這意味著對于此類事件,capture
?標(biāo)志是無用的,因?yàn)樗惺录陕犉鞫紝⒃凇?em>目標(biāo)階段”,因?yàn)樗鼈儠谀牟东@處理程序之前觸發(fā)。您無法停止將事件傳播到先前設(shè)置的處理程序”無論如何,所以
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>
請注意,您可以停止 Elements 的傳播。?模糊事件,但不模糊Window事件。
您必須找到其他方法來阻止當(dāng)前的偵聽器,也許是使用標(biāo)志變量?
- 1 回答
- 0 關(guān)注
- 141 瀏覽
添加回答
舉報(bào)