将 iframe 元素转换为文本数据? - JavaScript

Turn iframe Elements Into Text Data? - JavaScript

使用 iframe 元素,

<iframe id="">
<html>
<head>
  <title></title>
<body>
<element attribute=""></element>
<etc... />
</body>
</html>
</iframe>

我如何获取 iframe 的所有子元素,不包括 iframe 元素本身,将元素复制为文本,并将该文本附加到带有 id 的 textarea 元素,所有元素都兼容:

<meta http-equiv="X-UA-Compatible" content="IE=8"> 

...使用 JavaScript.

非常感谢任何帮助。

如果您不是在谈论来自不同框架的方法之间的通信,那么 iframe 的托管位置并不重要。不考虑这个概念,如果你想访问一些 DOM 元素,可以使用 HTMLIFrame​Element​contentWindow 。客户端甚至允许使用此方法操作元素。 MDN

中解释了更多详细信息

The contentWindow property returns the Window object of an element. You can use this Window object to access the iframe's document and its internal DOM. This attribute is read-only, but its properties can be manipulated like the global Window object.

一个例子

假设您有一个 iframe 及其影子 DOM 树,并且您想要访问使用 foo id 声明的 span 元素。

// HTMLIFrame​Element 
let someIframe = document.getElementBy("idOfIframe");

// Global Object of the Iframe
let frameWindow = someIframe.contentWindow;

// Dom Tree of the Target Window
let domTreeofFrame = frameWindow.document;

let textSpan = domTreeofFrame.getElementById("foo");