正如eicto評論所建議的那樣,可以使用jquery在ready事件中填充iframe。爲了調整iframe來的內容的高度的高度有些髒的黑客都必須適用,但代碼我結束了使用或多或少:
HTML
<!-- IMPORTANT! Do not add src or srcdoc -->
<!-- NOTICE! Add border:none to get a more "seamless" integration -->
<iframe style="border:none" scrolling="no" id="myIframe">
Iframes not supported on your device
</iframe>
JS
// Wait until iFrame is ready (body is then available)
$('#myIframe').ready(function() {
var externalHtml = '<p>Hello World!</p>';
// Find the body of the iframe and set its HTML
// Add a wrapping div for height hack
// Set min-width on wrapping div in order to get real height afterwords
$('#myIframe').contents().find('body')
.html('<div id="iframeContent" style="min-width:'+$('body').width()+'px">'
+externalHtml
+'</div>'
);
// Let the CSS load before getting height
setTimeout(function() {
// Set the height of the iframe to the height of the content
$('#myIframe').css('height',
$('#myIframe').contents()
.find('#iframeContent').height() + 'px'
);
},50);
});
更改編輯器?網上有很多編輯器 –
也可能你喜歡這個例子,它演示瞭如何在同一個源代碼上使用iframe。 http://jsfiddle.net/oceog/r4VPr/ –
謝謝,這做了一些修改的伎倆! – Sindre