我正在尝试将 iframe
内容作为文本获取:
var OCRTextframe='<iframe id="ocrtext" align="middle" src="http://localhost:...."/>';
OCRText.innerHTML=OCRTextframe;
var myIFrameBody = jQuery(document.getElementById('ocrtext')).contents().find('body').text();
console.log("iframe body text : "+myIFrameBody.length);
我的 iframe
已成功加载,但 myIFrameBody
是长度为 0 的空字符串,我是不是错过了什么?
我尝试在负载处理程序上实现但徒劳无功:
$('#ocrtext').load(function(){
var myIFrameBody = $(this).contents().find('body').text();
console.log("iframe body text : "+myIFrameBody);
});
我们将不胜感激。
最佳答案
附加处理程序后需要设置src
var OCRTextframe = '<iframe id="ocrtext" align="middle" />';
OCRText.innerHTML = OCRTextframe;
var ocrTextDocument = jQuery(document.getElementById('ocrtext'))
ocrTextDocument.on("load", function () {
var myIFrameBody = ocrTextDocument.contents().find('body').text();
console.log("iframe body text : " + myIFrameBody.length);
})
ocrTextDocument.attr("src", "http://localhost:....")
关于javascript - iframe 内容正文文本返回空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29921548/