【问题标题】:Adding attribute to iframe's body works in Chrome but not Firefox向 iframe 的正文添加属性在 Chrome 中有效,但在 Firefox 中无效
【发布时间】:2015-11-09 22:27:57
【问题描述】:

有谁知道为什么会这样——也就是说,添加了属性——在 Chrome 中,而不是在 Firefox 中?

this.wrapper.appendChild(this.ifr = document.createElement('iframe'));
this.ifr_doc = this.ifr.contentDocument || this.ifr.document;
this.ifr_doc.documentElement.querySelector('body').setAttribute('foo', 'bar'); //GRR...

没有错误;它只是在 Chrome 中,在检查器中,属性会显示,而在 Firefox (v 41.0.2) 中则不会。

(this.wrapper 是一个容纳 iframe 的容器)

【问题讨论】:

  • 检查你的this.ifr_doc.documentElement.querySelector('body')元素,它是否在ff中定义?
  • 是的,我查过了。

标签: javascript iframe document


【解决方案1】:

试试这个:

 if (this.ifr_doc.readyState == 'complete') {      
     this.ifr_doc.body.setAttribute('foo', 'bar'); 
 }

【讨论】:

  • 有趣 - 文档总是以未初始化的形式返回,即使我稍后通过超时再次检查。那我该如何初始化呢? +1。
  • 通过阅读 MDN,我不会 - 不能 - 手动初始化它 - 只有在 readystatechange 或 domcontentloaded 事件触发时才会初始化它。由于我正在重新创建一个新的 iframe 和文档,所以我不确定我可以在这里做什么。也许我应该把它作为一个单独的问题发布。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
  • 2016-02-19
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多