翻了翻message事件的属性,没找到可以直接获取事件源iframe的属性,想想也没毛病,毕竟事件也可以是其它窗口post过来的。于是想了个曲线方法。
先让发送源获取焦点,然后获取焦点元素。
window.addEventListener('message',function(msg){ msg.source.focus(); //做一些事来判断是不是某个iframe发送的消息 var sourceFrame=document.activeElement; });