Javascript—如何在使用iFrame、使用dom而不是jquery时更改页面内的元素内容
我有这个iframe,正如你所看到的,它使用onload触发器调用了一个js函数Javascript—如何在使用iFrame、使用dom而不是jquery时更改页面内的元素内容,javascript,dom,Javascript,Dom,我有这个iframe,正如你所看到的,它使用onload触发器调用了一个js函数 <iframe name="top" id="top" width="99%" height="20%" src="top.htm" frameborder="0" scrolling="no" onload="log_in()"></iframe> 我需要做的是从该函数中影响“top.htm”(更改innerHTML之类的内容)中的元素 但问题是函数不能识别“top.htm”页面的元
<iframe name="top" id="top" width="99%" height="20%" src="top.htm" frameborder="0" scrolling="no" onload="log_in()"></iframe>
我需要做的是从该函数中影响“top.htm”(更改innerHTML之类的内容)中的元素
但问题是函数不能识别“top.htm”页面的元素,只能识别index.htm(带有iFrame的页面)中的元素
p、 我必须使用DOM和iFrame
有人知道怎么做吗
10x:-)例如,访问所有dom元素:
document.getElementById("top").contentWindow.document.getElementsByTagName("*")
是您的问题吗?Iframe窗口及其文档 可以使用name属性通过frames数组获得对iframe生成的窗口和加载到iframe中的文档的引用
window.frames[iframeName]
window.frames[iframeName].document
引用的frames数组方法有广泛的支持,即使是在非常旧的浏览器中,只要将name属性附加到iframe。为获得最佳结果,请同时使用名称和id
对于更多最新浏览器,还可以通过iframe元素的contentWindow(IE win)和contentDocument(DOM)属性引用iframe中的文档:
// IE5.5+ windows
document.getElementById(iframeId).contentWindow
document.getElementById(iframeId).contentWindow.document
或者
从
这里的示例:Perfect,10倍,只需将getElementsByTagName转换为getElementById,效果非常好。。。10x:-)
// DOM
document.getElementById(iframeId).contentDocument