如何在Javascript中获取iframe的正文内容?

omg*_*omg 146 html javascript iframe

<iframe id="id_description_iframe" class="rte-zone" height="200" frameborder="0" title="description">
  <html>
    <head></head>
    <body class="frameBody">
      test<br/>
    </body>
  </html>
</iframe>
Run Code Online (Sandbox Code Playgroud)

我想得到的是:

test<br/>
Run Code Online (Sandbox Code Playgroud)

alg*_*thm 149

确切的问题是如何使用纯JavaScript而不是jQuery来实现它.

但我总是使用jQuery源代码中可以找到的解决方案.它只是一行原生JavaScript.

对我来说这是最好的,简单的可读性,甚至据我所知,以获得I帧内容的最短途径.

首先得到你的iframe

var iframe = document.getElementById('id_description_iframe');

// or
var iframe = document.querySelector('#id_description_iframe');
Run Code Online (Sandbox Code Playgroud)

然后使用jQuery的解决方案

var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
Run Code Online (Sandbox Code Playgroud)

它甚至可以在Internet Explorer中工作,它contentWindowiframe对象的属性中执行此操作.大多数其他浏览器使用该contentDocument属性,这就是我们在此OR条件中首先证明此属性的原因.如果没有设置试试contentWindow.document.

在iframe中选择元素

然后你通常可以使用getElementById()甚至querySelectorAll()从以下位置选择DOM-Element iframeDocument:

if (!iframeDocument) {
    throw "iframe couldn't be found in DOM.";
}

var iframeContent = iframeDocument.getElementById('frameBody');

// or
var iframeContent = iframeDocument.querySelectorAll('#frameBody');
Run Code Online (Sandbox Code Playgroud)

在iframe中调用函数

获取window元素iframe来调用一些全局函数,变量或整个库(例如jQuery):

var iframeWindow = iframe.contentWindow;

// you can even call jQuery or other frameworks
// if it is loaded inside the iframe
iframeContent = iframeWindow.jQuery('#frameBody');

// or
iframeContent = iframeWindow.$('#frameBody');

// or even use any other global variable
iframeWindow.myVar = window.myVar;

// or call a global function
var myVar = iframeWindow.myFunction(param1 /*, ... */);
Run Code Online (Sandbox Code Playgroud)

注意

如果您遵守同源政策,所有这一切都是可能的.

  • 请原谅,但代码行是jquery代码的摘录,代码块是我自己的示例代码.其他人应该让自己出局.你唯一缺少的是变量iframe.而且我不可能知道你的iframe在哪里或者ID是多少. (13认同)
  • 如果 iframe 有远程 src,您能否在答案的顶部添加一个警告,说明这不起作用?如果我错了,如果您能指出我如何在不发送另一个 HTTP 请求的情况下仍然可以获得 iframe 内容,我将不胜感激(不可能,因为我需要执行 javascript 来构建 iframe 内容并且不想部署如果可以避免,则使用第二个 javascript 环境)。 (6认同)
  • 为了使上述工作正常进行,我必须将其包含在 `document.querySelector('#id_description_iframe').onload = function() {...` 希望它对某人有所帮助。 (3认同)
  • iframe在这里是未定义的,要么你应该编写完整的代码,要么不应该用它来编写jQuery (2认同)

小智 68

使用JQuery,试试这个:

$("#id_description_iframe").contents().find("body").html()
Run Code Online (Sandbox Code Playgroud)

  • 它不起作用,因为*"域,协议和端口必须匹配."*:不安全的JavaScript尝试使用URL访问帧 (26认同)
  • 如上所述,如果域匹配,它将起作用.仅供参考,我刚发现$("#id_description_iframe #id_of_iframe_body").html()在Chrome中有效,但在所有firefox版本中都没有,因此使用上述功能很好.即$("#id_description_iframe #id_of_iframe_body").contents().find("body").html()确实在两者中都有效 (2认同)

biz*_*zr3 38

它对我来说很完美:

document.getElementById('iframe_id').contentWindow.document.body.innerHTML;
Run Code Online (Sandbox Code Playgroud)

  • 这很奇怪,但是对我来说`.body`不起作用。但是`.getElementsByTagName('body')[0]`可以。 (2认同)
  • 如果您使用 vanilla javascript,这应该是答案。 (2认同)

Jos*_*lio 23

AFAIK,iframe不能以这种方式使用.您需要将其src属性指向另一个页面.

以下是使用平面旧javascript获取其正文内容的方法.这适用于IE和Firefox.

function getFrameContents(){
   var iFrame =  document.getElementById('id_description_iframe');
   var iFrameBody;
   if ( iFrame.contentDocument ) 
   { // FF
     iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0];
   }
   else if ( iFrame.contentWindow ) 
   { // IE
     iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0];
   }
    alert(iFrameBody.innerHTML);
 }
Run Code Online (Sandbox Code Playgroud)

  • 它在 safari(即分支)上运行 (2认同)

Cou*_*Web 10

content在JS中使用iframe:

document.getElementById('id_iframe').contentWindow.document.write('content');
Run Code Online (Sandbox Code Playgroud)


小智 5

我认为在标签之间放置文本是为无法处理 iframe 的浏览器保留的,即..

<iframe src ="html_intro.asp" width="100%" height="300">
  <p>Your browser does not support iframes.</p>
</iframe>
Run Code Online (Sandbox Code Playgroud)

您使用 'src' 属性来设置 iframes html 的源...

希望有帮助:)


Gra*_*ark 4

Chalkey是正确的,您需要使用src属性来指定iframe中要包含的页面。如果您执行此操作,并且 iframe 中的文档与父文档位于同一域中,则可以使用以下命令:

var e = document.getElementById("id_description_iframe");
if(e != null) {
   alert(e.contentWindow.document.body.innerHTML);
}
Run Code Online (Sandbox Code Playgroud)

显然,您可以对这些内容做一些有用的事情,而不仅仅是将它们放入警报中。