从 iframe 获取文本 [英] Get Text from iframe

查看:25
本文介绍了从 iframe 获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 HTML

<iframe id ="myIframe">
     #document
         <head>...</head>
        <body>Text I want to get is here</body>
</iframe>

我只知道 iframe 的 ID.使用 JavaScript,如何从正文中提取文本?

I only know the iframe's id. Using JavaScript, how can I extract the text from the body?

推荐答案

假设您的 iframe 与您的 HTML 在同一个域中:

Assuming your iframe is in the same domain as your HTML:

var myIFrame = document.getElementById("myIframe");
var content = myIFrame.contentWindow.document.body.innerHTML;

否则,您将面临一些同源政策问题.

Otherwise you'll face some Same Origin Policy issues.

这篇关于从 iframe 获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆