0

我正在编写某种由 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!"); }

4

1 回答 1

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>
于 2013-05-16T02:46:40.253 回答