【问题标题】:Setting content of iframe using javascript fails in firefox在 Firefox 中使用 javascript 设置 iframe 的内容失败
【发布时间】:2014-09-01 09:54:27
【问题描述】:

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

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

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

它在谷歌浏览器中运行良好,但在 Firefox 中显示内容片刻然后消失。 请帮我解决它。

【问题讨论】:

  • 那是我的打字错误。

标签: javascript jquery html firefox iframe


【解决方案1】:

我遇到了同样的问题。我在 Firefox 中看到了 iframe 的加载功能,但在 chrome 中没有。所以 firefox 在 load 事件时重新加载 iframe。

所以用你现有的代码试试这个:-

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

    })

【讨论】:

  • 它有效,但是两次编写相同的代码是一种好习惯吗?
  • @DeadlyDemon 那么你应该重构它并将它移动到一个单独的函数中。写function foo() {$(iframe).contents().find('html').html(content);}:),然后从两个地方调用foo()。为帖子 +1。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 2010-12-03
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
  • 1970-01-01
相关资源
最近更新 更多