我正在编写某种由iframe组成的显示器。当我点击iframe外部的按钮时,它应该在iframe上向下。有可能吗?
<iframe src = "display.html" id = "ifr"></iframe>
<div class = "buttons" onClick = "showSequence()">
Get the whole sequence
</div>
javascript代码:
function showSequence(){
var iframe = document.getElementById("ifr");
(iframe.contentDocument || iframe.contentWindow.document).write("Something!");
}
答案 0 :(得分:2)
是的,如果iframe位于同一个域中。 这是一个代码示例:
<button onclick="test()">Click to write in iframe</button>
<iframe id='ifr'></iframe>
<script>
function test(){
var iframe = document.getElementById("ifr");
//contentDocument because IE8 doesn't support contentWindow
(iframe.contentDocument || iframe.contentWindow.document).write("hello iframe");
}
</script>