質問

後でhtmlを挿入するためのプレースホルダーとして空のiframeを作成できますか?

言い換えれば、idを持つ空のiframeがあるとします、

htmlを挿入するにはどうすればよいですか

jqueryを使用していますが、それにより簡単になります。

役に立ちましたか?

解決

jQueryなしでも実行できます:

var iframe = document.getElementById('iframeID');
iframe = iframe.contentWindow || ( iframe.contentDocument.document || iframe.contentDocument);

iframe.document.open();
iframe.document.write('Hello World!');
iframe.document.close();

jQueryのhtmlは、挿入されたHTMLからbody、html、およびheadタグを取り除きます。

他のヒント

このページのソースを見る: http://mg.to/test/dynaframe.htmlやりたいことを正確に実行しているように見えます。

$(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 );
});

いいえ、そうではありません。次のようにスクリプトを変更します。

$(function() {
    var $frame = $('iframe');
    setTimeout( function() {
            var doc = $frame[0].contentWindow.document;
            var $body = $('body',doc);
            $body.html('<h1>Test</h1>');
    }, 1 );
});
iframeElementContainer =   document.getElementById('BOX_IFRAME').contentDocument;
iframeElementContainer.open();
iframeElementContainer.writeln("<html><body>hello</body></html>");
iframeElementContainer.close();

はい、それが可能であることはわかっていますが、主な問題は、外部からフレームを処理できないことです。すでに他の何かをロードしています。

$(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 );
});

ただし、開始する場合

<iframe src="http:/www.hamrobrt.com">
<---Your Script to put as you like--->

それを打つことは不可能です。

  

同じ問題があり、iframeのSRC属性なしでデータベースからiframeにhtmlコードスニペットを動的に表示する必要があり、次のコードで同じ問題を修正しました

     

これが、私と同じ要件を持つ人の助けになることを願っています。

jsfiddleの例はこちら

HTML:

<iframe src="" frameborder="0" class="iframe"></iframe>
<iframe src="" frameborder="0" class="iframe"></iframe>
<iframe src="" frameborder="0" class="iframe"></iframe>
<iframe src="" frameborder="0" class="iframe"></iframe>

JS

<script>
var doc,$body;
var txt = Array(
            '<style>body{background-color:grey} h1{background-color:red;font-weight:900}</style><h1>Cool!!! this is text for</h1><p>First Iframe</p>',
            '<style>body{background-color:pink}h1{background-color:green;font-weight:200}</style><h1>Cool This is Text for</h1><p> second Iframe',
            '<style>body{background-color:yellow}h1{font-weight:400}</style><h1>Cool..... This is text FOR : </h1> <div>3rd Iframe</div>',
            '<style>body{background-color:blue} h1{font-weight:400}</style><h1>Cool This is text for Fourth iframe</h1>'
            );
$('.iframe').each(function(index,value){
    doc = $('iframe')[index].contentWindow.document;
        $body = $('body',doc);
        $body.html(txt[index]);
});
</script>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top