Firefox无法找到iframe [英] Firefox not able to find iframe
问题描述
这是我尝试访问的iframe:
< div class =mceBodyid =additionalTxt_b >
< / div>
使用此行:
frames ['additionalTxt_f']。document.getElementsByTagName(body)[0] .innerHTML
出于某种原因,我从firebug中获得了frames.additionalTxt_f未定义。
我在其他页面上有类似的iframe(由punyMCE插件动态创建),它们工作得很好。而且IE7 / 8在访问这个iframe时也没有问题。
这里完全没有问题。关于为什么Firefox无法找到iframe的任何想法?
window.frames []
数组由[i]框架的名称
属性(aka frame target)索引。不能依赖 id
来工作 - 虽然它可能在IE< 8中,这通常认为名称和id是同一件事。 如果您想通过ID访问框架内容,请使用DOM Level 2 HTML contentDocument
属性而不是old-school(DOM Level 0) frames
array:
document.getElementById('additionalTxt_f ').contentDocument.body.innerHTML
...但为了兼容IE< 8 ,因为它不支持 contentDocument
:
var f = document.getElementById('additionalTxt_f');
var d = f.contentDocument? f.contentDocument:f.contentWindow.document;
d.body.innerHTML
所以这取决于您认为哪种方法不那么难看:额外的脚本工作,或者只是使用名称
属性。
This is the iframe I'm trying to access:
<div class="mceBody" id="additionalTxt_b">
<iframe frameborder="0" id="additionalTxt_f" src='javascript:""' class="punymce"/>
</div>
Using this line:
frames['additionalTxt_f'].document.getElementsByTagName("body")[0].innerHTML
For some reason I'm getting "frames.additionalTxt_f is undefined" from firebug. I have similar iframes (dynamically created by punyMCE plugin) on other pages, and they work perfectly fine. And IE7/8 has no problem accessing this iframe either.
Just at a complete loss here. Any ideas on why Firefox can't find the iframe?
The window.frames[]
array is indexed by the [i]frame's name
attribute (aka frame target). id
can't be relied upon to also work — although it may in IE <8, which often thinks names and ids are the same thing.
If you want to access a frame's content via ID, use the DOM Level 2 HTML contentDocument
property instead of the old-school ("DOM Level 0") frames
array:
document.getElementById('additionalTxt_f').contentDocument.body.innerHTML
...but then, for compatibility with IE <8, you also have to add some fallback cruft, since it doesn't support contentDocument
:
var f= document.getElementById('additionalTxt_f');
var d= f.contentDocument? f.contentDocument : f.contentWindow.document;
d.body.innerHTML
So it's up to you which method you think is less ugly: the extra script work, or just using the name
attribute.
这篇关于Firefox无法找到iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!