How to use relatedTarget (or equivalent) in IE?

…衆ロ難τιáo~ 提交于 2020-03-21 12:01:08

问题


Apparently IE (11) has an issue with relatedTarget, for example on blur events. Is there an alternative for IE to get the relatedTarget?

Here is an example that produces an error in IE: https://jsfiddle.net/rnyqy78m/


回答1:


If I look at this list: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/relatedTarget blur isn't included as having a standard secondary target. So I assume the secondary target on blur in chrome is non-standard.

If you replace blur by focusin or focusout, it also works for IE11 for me.




回答2:


It looks like IE11 sets document.activeElement to the next focused element before the blur event is called. So to handle blur correctly in all browsers including IE11 you could use something like

var target = evt.relatedTarget;
if (target === null) { target = document.activeElement; }

I adapted your fiddle to use this: https://jsfiddle.net/hukt0rhj/



来源:https://stackoverflow.com/questions/41298948/how-to-use-relatedtarget-or-equivalent-in-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!