Javascript获取框架的全部内容

问题描述:

我想让一个脚本重写一个简单的文本文件,这个文件文件被打开作为一个框架的源 - 一个没有任何html的文件。我尝试了 frames [1] .document.innerHTML 但没有成功。我甚至尝试过 .outerHTML ,什么也没得到。我可以(a)从帧中获取帧的全部内容以使用脚本进行操作,然后 .write()将结果返回到帧吗?或者(b)添加< html> < div> 标记,包装文档以便我可以获取内部html然后操纵那个?任何帮助表示感谢。

I want to have a script rewrite a simple text-file opened up as the source of a frame -- one that lacks any html. I tried frames[1].document.innerHTML but without success. I even tried .outerHTML and got nothing. Can I either (a) get from the frame the entire contents of the frame to manipulate using the script and then .write() back the result to the frame? Or (b) add <html> and <div> tags, wrapping the document so I can get the inner html and then manipulate that? Any help appreciated.

只要你的主页面和iFrame页面跟随同源策略,你可以像这样访问frame的html:

As long as your main page and iFrame page follow Same Origin policy, you can access frame's html like this:

window.frames['iframe01'].document.body.innerHTML

window.frames['iframe01'].contentDocument.documentElement.innerHTML

写:

window.frames['iframe01'].contentDocument.write("[content here]");