简体   繁体   中英

Is it possible to write something in a iframe when a button outside the iframe is clicked?

I am coding some kind of display made of an iframe. When I click a button, which is outside the iframe, it should right something down on the iframe. Is it possible?

<iframe src = "display.html" id = "ifr"></iframe>

    <div class = "buttons" onClick = "showSequence()">
        Get the whole sequence
    </div>

The javascript code:

function showSequence(){ var iframe = document.getElementById("ifr"); (iframe.contentDocument || iframe.contentWindow.document).write("Something!"); }

Yes, if the iframe is in the same domain. Here is a code example:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM