[英]javascript or jquery code to obtain entire text content of web page within an iframe (including/excluding links on that page)
[英]how to obtain entire text content of a web page within a iframe using javascript
我想獲取iframe網頁中包含的文本。
根據關於SO的現有問題,以下給出了在iframe中獲取網頁內容的JavaScript代碼-
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中網頁的文本內容?
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.