网页中获取IFrame内容

使用下面的函数可以直接取得IFrame里的标签
<script type="text/javascript">
function getElementByIdInIFrame(objIFrame, idInIFrame)
{
    var obj;
    if (objIFrame.contentDocument) // For NS6
        obj = objIFrame.contentDocument.getElementById(idInIFrame);
    else if (objIFrame.contentWindow)   // For IE5.5 and IE6
        obj = objIFrame.contentWindow.document.getElementById(idInIFrame);
    else if (objIFrame.document)    // For IE5
        obj = objIFrame.document.getElementById(idInIFrame);
    return obj;
}
</script>

例子:
<img src="images/wait_up.gif" alt="图片等待上传..." id="pic1" /><br />
<iframe src="../aspx/upff.aspx" width="380px;" frameborder="0" height="70px" id="ii" name="ii" onload="getpic();"></iframe>
       <script type="text/javascript">
            function getpic()
            {
                               var aa = getElementByIdInIFrame(document.getElementById("ii"), "aa");
                               
                if (aa != null)
                {
                                   document.getElementById("pic1").src = aa.value;
                               }
            }
         </script>

参考:
<script>
//因WINDOWS的安全机制,只能读取相同域名下的iframe内容
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;
}
</script>

<iframe src="test.htm" id="frmIn" name="frmIn"></iframe>
<input type="button" value="click" onclick="alert(GetIframeInnerHtml(document.all.frmIn))">

posted @ 2012-08-13 23:03  weiwei~  阅读(2017)  评论(0编辑  收藏  举报