javascript Javascript获取框架的全部内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6770704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:48:16  来源:igfitidea点击:

Javascript to get entire contents of frame

javascript

提问by thf

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.innerHTMLbut without success. I even tried .outerHTMLand 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.

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

回答by Mrchief

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

只要您的主页和 iFrame 页面遵循同源策略,您就可以像这样访问框架的 html:

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

or

或者

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

To write:

来写:

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