【问题标题】:iframe content body text return empty stringiframe 内容正文文本返回空字符串
【发布时间】:2015-04-28 13:53:39
【问题描述】:

我正在尝试以文本形式获取 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);
        });

任何帮助将不胜感激。

【问题讨论】:

  • jQuery(document.getElementById('ocrtext')) wtf?为什么不#ocrtext
  • 在ajax查询成功函数内
  • 尝试使用filter() 而不是find()
  • @charlietfl 结果相同!
  • 必须在负载处理程序中,需要确保首先呈现内容,那里的结果相同?

标签: javascript jquery html iframe


【解决方案1】:

附加处理程序后需要设置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:....")

【讨论】:

  • 感谢您的回复,我试了一下,但这似乎是一个安全问题,就像@charlietfl 所说:Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "http://localhost" from accessing a frame with origin "http://localhost:8888". Protocols, domains, and ports must match.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 2016-08-27
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多