<body onload="onload();">
function onload()
{
document.onkeypress = function(e) {
//e.preventDefault(); // Prevent any default browser behaviour.
console.log('send: '+e);
document.getElementById('iframe').contentWindow.postMessage ('Hello Treehouse!', '*my*');
}
window.addEventListener('message', function(event) { console.log('get: '+event); }, false);
}
</script>
我想将按键转发给iframe,但到目前为止它不起作用,iframe没有收到消息
答案 0 :(得分:2)
当您在iFrame中实际需要时,看起来您正试图将事件绑定在与发件人相同的窗口中。
即。你的iFrame中有这段代码:
<script>
window.addEventListener('message', function(event) { console.log('get: '+event); }, false);
</script>