如何使用javascript在iframe中获取网页的整个文本内容 [英] how to obtain entire text content of a web page within a iframe using javascript

查看:55
本文介绍了如何使用javascript在iframe中获取网页的整个文本内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取iframe网页中包含的文本.

I want to obtain the text contained within a web page in an iframe.

根据关于SO的现有问题,以下给出了在iframe中获取网页内容的javascript代码-

As per an existing question on SO, the javascript code to obtain the content of a web page in an iframe is given below--

var myIFrame = document.getElementById(iFrameName); // Creating an object of the iframe
var content = myIFrame.contentWindow.document.body.innerHTML; // Getting it's content into a variable

问题是我只想在iframe中获取网页的文本内容-我不想通过获取全部内容然后通过解析来删除图像/链接等来做到这一点...上面的代码在网页的主体内容中包含HTML标记---是否可以通过某种方式仅在iframe中获取网页的文本内容?

The problem is that I want to obtain only the text content of the web page in iframe- I dont want to do this by obtaining the entire content and then parsing through it to remove images/links etc...The code above contains HTML Markup in body content of the web page--- Is there some way to obtain only the text content of web page in an iframe?

推荐答案

var myIFrame = document.getElementById(iFrameName); // Creating an object of the iframe
var myIFrameBody = myIFrame.contentWindow.document.body; // Getting it's body content into a variable    

function getStrings(n, s) {
   var txt, childNodes, child, max, m, i;

   if (n.nodeType === 3) {
      txt = trim(n.data);

      if (txt.length > 0) {
         s.push(txt);
      }
   } 
   else if (n.nodeType === 1) {
      for (i = 0, max = n.childNodes.length; i < max; i++) {
         child = n.childNodes[i];
         getStrings(child, s);
      }
   }
}

/**
 Extract the html text starting from a node n.
 */
function getText(n) {
   var s = [],
       result;

   getStrings(n, s);
   result = s.join(" ");

   return result;       
}

var myIFrameText = getText(myIFrameBody);

这篇关于如何使用javascript在iframe中获取网页的整个文本内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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