我可以创建一个空的 iframe 作为占位符,以便稍后将 html 插入其中吗?
换句话说,假设我有一个带有 id 的空 iframe,
如何在其中插入 html?
我正在使用 jquery,如果这样更容易的话。
我可以创建一个空的 iframe 作为占位符,以便稍后将 html 插入其中吗?
换句话说,假设我有一个带有 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--->
那么它不可能击中那个。