空iFrame加载行为是不同的

Empty iFrame load behaviour is different

本文关键字:是不同 iFrame 加载      更新时间:2023-09-26

有没有人能帮我确定为什么这个iframe在Chrome vs Firefox中的行为不同?

$('<iframe id="iframe1"></iframe>').appendTo($('.main')).contents().find('html').html("<h1 style='text-align: center;'>This IS an iframe</h1>");
http://jsfiddle.net/ioowilly/L9uAX/

或者试试这个

$('<iframe id="iframe1" src="javascript:undefined;" ></iframe>').appendTo($('.main')).contents().find('body').html("<h1 style='text-align: center;'>This IS an iframe</h1>");

这是因为一些导航器需要一些时间来加载DOM中的iframe

这是补丁:

$('<iframe id="iframe1"></iframe>').appendTo($('.main')).ready(function() {
    setTimeout(function() {
$('#iframe1').contents().find('body').append('<h1 style="text-align: center;">This IS an iframe</h1>');
    }, 50);
});

基于这个答案:将内容附加到动态创建的iframe中会得到一个空iframe