我正在尝试使用 JavaScript 设置 iframe 的内容。
我有 html 字符串。我正在写:-
var iframe = $('iframe')[0],
content = '<div>test</div>';
$(iframe).contents().find('html').html(content);
它在谷歌浏览器中工作正常,但在火狐中显示内容一会儿然后消失。 请帮我解决这个问题。
最佳答案
我也遇到了同样的问题。我看到 iframe 的加载功能在 Firefox 中触发,但在 chrome 中却没有。因此,Firefox 在加载事件时再次重新加载 iframe。
因此,使用您现有的代码尝试以下操作:-
$(iframe).load(function(e){
$(iframe).contents().find('html').html(content);
})
关于javascript - 在 Firefox 中使用 javascript 设置 iframe 内容失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24686443/