因为前面的图片上传用到了iframe,这里记录一下用js获取iframe内容的代码,尤其当iframe的src为图片时:
function GetIframeInnerHtml(objIFrame)
{
var iFrameHTML = "";
if (objIFrame.contentDocument)
{
// For NS6
iFrameHTML = objIFrame.contentDocument.innerHTML;
}
else if (objIFrame.contentWindow)
{
// For IE5.5 and IE6
iFrameHTML = objIFrame.contentWindow.document.body.innerHTML;
}
else if (objIFrame.document)
{
// For IE5
iFrameHTML = objIFrame.document.body.innerHTML;
}
return iFrameHTML;
}
<iframe src= "kitty.jpg" id= "frmIn" name= "frmIn" scrolling= "no" width= "450" height= "450"></iframe>
<input type= "button" value= "click" οnclick= "alert(GetIframeInnerHtml(document.all.frmIn))">
{
var iFrameHTML = "";
if (objIFrame.contentDocument)
{
// For NS6
iFrameHTML = objIFrame.contentDocument.innerHTML;
}
else if (objIFrame.contentWindow)
{
// For IE5.5 and IE6
iFrameHTML = objIFrame.contentWindow.document.body.innerHTML;
}
else if (objIFrame.document)
{
// For IE5
iFrameHTML = objIFrame.document.body.innerHTML;
}
return iFrameHTML;
}
<iframe src= "kitty.jpg" id= "frmIn" name= "frmIn" scrolling= "no" width= "450" height= "450"></iframe>
<input type= "button" value= "click" οnclick= "alert(GetIframeInnerHtml(document.all.frmIn))">
document.all.frmIn.contentWindow.document.body.innerHTML
frmIn是iframe的id
在FF里获取到的内容是这样的:
![](https://i-blog.csdnimg.cn/blog_migrate/0e5e377d5a6b37bbb0f5dbf448d08e3b.jpeg)
在IE里获取到的内容是这样:
![](https://i-blog.csdnimg.cn/blog_migrate/ec402e90917db317309839dea57ac15a.jpeg)
差不多了,这样!
转载于:https://blog.51cto.com/katrina/143636