2009-03-06 10 views

Odpowiedz

30

Można zrobić to bez jQuery także:

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 paski html body, html i szef tagi z włożonej HTML.

+0

To jest poprawna odpowiedź. ''. Zauważ, że element iframe musi zostać wstawiony do dokumentu nadrzędnego. –

+0

Działający jak zaklęcie podczas ładowania skryptów "document.write" z żądań AJAX –

+2

'iframe = iframe.contentWindow || (iframe.contentDocument.document || iframe.contentDocument); ' – Nildarar

36

Wyświetl źródło tej strony: http://mg.to/test/dynaframe.html Wygląda na to, że robi dokładnie to, co chcesz zrobić.

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

To tylko kopiuje 'organ do spraw Element, kiedy może potrzebować' head' elementu lub nawet atrybuty elementu 'html'. – Flimm

1

Tak wiem, że jest to możliwe, ale głównym problemem jest to, że nie możemy obsługiwać ramki spoza niej i już załadowany jakąś inną rzecz.

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

ale jeśli zaczniemy jako

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

następnie jego niemożliwe uderzył się.

8

Nie, nie jest. Można by zmodyfikować skrypt tak:

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

Mam ten sam problem, gdzie mam pokazać kod HTML fragment w iframe dynamicznie z bazy danych bez atrybutu src iframe i naprawiłem sam problem z następującym kodem

Mam nadzieję, że komuś pomóc kto ma taki sam wymóg jak ja.

jsfiddle example here

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> 
Powiązane problemy