contentDocument 和 contentWindow javascript iframe/frame 访问属性之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17197084/
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-27 07:26:46  来源:igfitidea点击:

Difference between contentDocument and contentWindow javascript iframe/frame access properties

javascriptiframeframe

提问by Samuel Lopez

  • What is the difference between these two properties (contentDocument and contentWindow)?
  • Is there any difference in the way they acces the content in the frame/iframe?
  • Is there any performance issues?
  • Should I use contentXXXXX.document.getElementsBy...or contentXXXXX.getElementsBy...?
  • 这两个属性(contentDocument 和 contentWindow)有什么区别?
  • 他们访问 frame/iframe 中的内容的方式有什么不同吗?
  • 是否有任何性能问题?
  • 我应该使用contentXXXXX.document.getElementsBy...还是 contentXXXXX.getElementsBy...

I've been searching around in the web but haven't found too much information and before I did a big research I thought I could ask the great community here at stackoverflow. As a comment I'm doing a script that counts inputs on a web page, accessing all levels of frames or iframes and adding those inputs found inside the frames/iframes to the global count. All content is supposed to be from the same domain so no there should be no problems with same-origin-policy, I believe. For the script I'm using pure JavaScript no jQuery or any frameworks/libraries that make life easier u_u jaja. This is a practice script and if anyone would like a look at the code I'll post it, no problem. Thanks for anyone who takes the time to explain this!

我一直在网上搜索,但没有找到太多信息,在我进行大规模研究之前,我想我可以在 stackoverflow 上询问这里的优秀社区。作为评论,我正在编写一个脚本来计算网页上的输入,访问所有级别的框架或 iframe,并将这些在框架/iframe 中找到的输入添加到全局计数中。所有内容都应该来自同一个域,所以我相信同源策略应该没有问题。对于脚本,我使用纯 JavaScript 没有 jQuery 或任何框架/库,使生活更轻松 u_u jaja。这是一个练习脚本,如果有人想看一下代码,我会发布它,没问题。感谢任何花时间解释这一点的人!

回答by Felix Kling

I think the <iframe>MDN documentationexplains it well:

我认为<iframe>MDN 文档解释得很好:

From the DOM iframeelement, scripts can get access to the windowobject of the included HTML page via the contentWindowproperty. The contentDocumentproperty refers to the documentelement inside the iframe(this is equivalent to contentWindow.document), but is not supported by Internet Explorer versions before IE8.

从 DOMiframe元素,脚本可以window通过contentWindow属性访问包含的 HTML 页面的对象。该contentDocument属性指的是document内部的元素iframe(这相当于contentWindow.document),但 IE8 之前的 Internet Explorer 版本不支持。

So you would use contentWindowthe same way you use windowand contentDocumentas you use document.

所以,你会用contentWindow你用同样的方法window,并contentDocument为你使用document