编辑:
修复如下,只需将高度和宽度设置为零即可隐藏 iframe:
<iframe frameborder="0" id="frm" height="0" width="0"></iframe>
var frmurl ='http://www.bbc.co.uk';
jQuery(function($){
$('#frm').attr('src', frmurl);
var doc = $('#frm')[0].contentDocument;
$(doc.body).html('This text should replace the BBC website');
});
我的网站依赖第三方系统来更新数据库中的记录并显示确认消息。通过 iFrame 访问第三方系统。我想用我自己的确认消息替换确认消息。
我使用 jQuery,并且 iFrame 工作正常,只是在第三方系统生成的确认消息覆盖它之前,我的替换文本显示得非常短暂。你可以看一个例子at this jsFiddle 。
如何确保我自己的文本不被覆盖?我认为这与生成页面的事件顺序有关?
<iframe frameborder="0" id="frm" scrolling="no" width="100%"></iframe>
var frmurl ='http://www.bbc.co.uk';
jQuery(function($){
$('#frm').attr('src', frmurl);
var doc = $('#frm')[0].contentDocument;
$(doc.body).html('This text should replace the BBC website');
});
最佳答案
加载 IFRAME 是异步的。您也许可以像这样做您想做的事:
jQuery(function($){
$('#frm').attr('src', frmurl).load(function() {
var doc = $(this)[0].contentDocument;
$(doc.body).html('This text should replace the BBC website');
});
});
这将等待 IFRAME 加载,然后尝试修改它。
但是,我不确定这是否会被允许 - 您不允许操作从不同域加载的 IFRAME 的内容。
更新:
我是对的,这不起作用。我试过这个fiddle和控制台日志:
Blocked a frame with origin
"http://fiddle.jshell.net"
from accessing a frame with origin"http://www.bbc.co.uk"
. Protocols, domains, and ports must match.
你不被允许做你想做的事。
关于javascript - jQuery - 页面加载顺序(?)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18575281/