Insert HTML into iframe

前端 未结 4 1470
终归单人心
终归单人心 2020-12-29 05:05

I am creating a in browser HTML editor. I am using the syntax highlighter called Code Mirror which is very good.

My setup is an iframe that holds a view of the page

相关标签:
4条回答
  • 2020-12-29 05:37

    You can do it all on one line with jquery

    $("iframe").contents().find('html').html("Your new HTML");
    

    See working demo here http://jsfiddle.net/dWpvf/972/

    Replace "iframe" with "#render" for your example. I left in "iframe" so that other users can reference the answer.

    0 讨论(0)
  • 2020-12-29 05:47

    Set the document.designMode property and then try adding the HTML.

    See also: Rich-Text Editing in Mozilla

    0 讨论(0)
  • 2020-12-29 05:50

    This problem bugged me too and finally found solution.

    Its late but will add value to other visitors who might need proper fix.

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html lang="en">
    <head>
        <title>Iframe test</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js">
        </script>
        <script type="text/javascript">
            $(function() {
                var $frame = $('<iframe style="width:200px; height:100px;">');
                $('body').html( $frame );
                setTimeout( function() {
                    var doc = $frame[0].contentWindow.document;
                    var $body = $('body',doc);
                    $body.html('<h1>Test</h1>');
                }, 1 );
            });
        </script>
    </head>
    <body>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-29 05:52

    try

    iframe[0].documentElement.innerHTML = code;
    

    Assuming that the url to the iframe is from the same domain as the webpage.

    0 讨论(0)
提交回复
热议问题