将属性添加到iframe'的body可以在Chrome中使用,但不能在Firefox中使用

Adding attribute to iframe's body works in Chrome but not Firefox

本文关键字:Firefox Chrome 但不能 body 添加 属性 iframe      更新时间:2023-09-26

有人知道为什么这在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(41.0.2版本)中则不会。

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

试试这个:

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