【发布时间】:2014-01-29 22:50:55
【问题描述】:
我正在进行一项测试,试图将值发送到 iframe 中定义的函数(两个页面来自同一来源)。聚焦时 iframe 的内容会响应某些击键。我想要实现的是通过主页上的按钮从 iframe 触发这些按键操作。
iframe的内容是动态的,需要在不刷新的情况下运行,否则我知道我可以改变iframe src来携带变量。到目前为止我所拥有的......
父页面:
<!DOCTYPE html><html><head>
<style>
iframe{border:1px solid blue;height:100px;width:100px;margin:20px auto 0 auto;}
</style>
</head><body>
<iframe id="iframe" src="iframetest2.html"></iframe>
<button onclick="simKey(32);">CLICK</button>
<script>
var iframe=document.getElementById('iframe');
var ifContent=iframe.contentWindow
function simKey(x){
ifContent.postMessage(testScript(x),'*','[transfer]');
}
</script></body></html>
以及 iFrame 内容:
<!DOCTYPE html><html><head>
<script>
function testScript(x){
document.body.innerHTML=x;
setTimeout(function(){document.body.innerHTML='';},700);
}
</script></head><body onkeypress="testScript(event.keyCode)"></body></html>
如果可能的话,我也尽量避免使用 jQuery。这是一个概念网页设计,真的很想手写 Javascript,以便我可以根据需要进行修改。
【问题讨论】:
标签: javascript html dom iframe