在 Firefox 中使用 javascript 设置 iframe 内容失败

Dea*_*mon 2 html javascript iframe firefox jquery

我正在尝试使用 javascript 设置 iframe 的内容。

我有 html 字符串。我正在写:-

 var iframe = $('iframe')[0],
 content = '<div>test</div>';        
 $(iframe).contents().find('html').html(content);
Run Code Online (Sandbox Code Playgroud)

它在谷歌浏览器中工作正常,但在火狐中显示内容一会儿然后消失。请帮我解决它。

Ind*_*dra 5

我遇到了同样的问题。我看到 iframe 的加载功能在 Firefox 中触发,但在 chrome 中却没有。因此,Firefox 在加载事件时再次重新加载 iframe。

因此,使用您现有的代码尝试以下操作:-

   $(iframe).load(function(e){
      $(iframe).contents().find('html').html(content);

    })
Run Code Online (Sandbox Code Playgroud)