javascript - iframe 内容正文文本返回空字符串

标签 javascript jquery html iframe

我正在尝试将 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/

相关文章:

javascript - jQuery:if语句来选择一个选项值

javascript - Bootstrap Switch 触发 switchChange 事件但视觉上没有变化

javascript - 如何通过与多个类名匹配的类名来选择javascript中的元素?

javascript - VS 代码中的 "Could not find task ' npm '"错误

Javascript 框架和渐进增强

javascript - 将新属性附加到对象中的特定位置

javascript - 是否可以在 Canvas 内拖动图像并在同一 Canvas 上绘图?

jquery - 在 jQuery Mobile 中重新调整选择框的大小?

html - Webkit 转换缩放问题

Python:如何选取相邻元素?