看Vue源代碼內置指令時,發現了Vue對於IE9的selectionchange
事件作了特殊處理,這引發了個人興趣。原來Vue對全局的selectionchange
事件進行監聽,一旦發現就會嘗試觸發document.activeElement
當前文檔激活對象的input
事件。
具體代碼以下:javascript
src/core/util/env.js
java
export const inBrowser = typeof window !== 'undefined' export const UA = inBrowser && window.navigator.userAgent.toLowerCase() export const isIE = UA && /msie|trident/.test(UA) export const isIE9 = UA && UA.indexOf('msie 9.0') > 0
src/platforms/web/runtime/directives/model.js
web
if (isIE9) { document.addEventListener('selectionchange', () => { const el = document.activeElement if (el && el.vmodel) { trigger(el, 'input') } }) } function trigger (el, type) { const e = document.createEvent('HTMLEvents') e.initEvent(type, true, true) el.dispatchEvent(e) }
原來IE9
輸入框input事件
沒法監聽到鍵盤的backspace
鍵和delete
鍵對內容的改變,以及右鍵菜單的剪切、撤銷、刪除對內容的改變,用keyup
事件咱們能夠解決鍵盤backspace鍵delete鍵的問題,可是對於右鍵對於文本的操做仍是無能爲力,還好有selectionchange
事件,它能夠在文檔上的當前文本選擇被改變時觸發,例如文本選擇、剪切、刪除、粘貼等操做。瀏覽器
selectionchange
事件只能綁定在Document
接口對象上,其餘元素綁定無效,並且不能夠取消、也不能冒泡;Chrome
上右鍵菜單的剪切並不會觸發selectionchange
事件;