向 iframe 的正文添加属性适用于 Chrome,但不适用于 Firefox



有人知道为什么这在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'); 
 }

最新更新