Skip to main content
 首页 » 编程设计

c#之动态 IFrame 调整大小在 IE 中不起作用

2024年02月20日14over140

我有一些代码可以动态调整 iframe 的大小:

function autoResizeFrames() { 
    frames = document.getElementsByTagName("iFrame"); 
    window.setInterval("autoresize_frames()", 400); 
} 
 
function autoresize_frames() { 
 
    for (var i = 0; i < frames.length; ++i) { 
        if (frames[i].contentWindow.document.body) { 
            var frames_size = frames[i].contentWindow.document.body.offsetHeight; 
            if (document.all && !window.opera) { 
                frames_size = frames[i].contentWindow.document.body.scrollHeight; 
            } 
            frames[i].style.height = frames_size + 'px'; 
        } 
    } 
} 

此功能在Firefox和Chrome中效果很好,但在IE 10和9中效果不佳。

这是我有一个 IFrame 的地方

<div id="projectModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="projectModalLabel" 
            aria-hidden="true"> 
            <div class="modal-header"> 
                   <button type="button" runat="server" onserverclick="btnClose_Click"  class="close" aria-hidden="true"> 
                    ×</button> 
                <h3 id="projectModalLabel">Edit Project</h3> 
            </div> 
            <div class="modal-body"> 
                <iframe src="" style="width: 100%; height: 200px; border: none;" frameborder="0" id="projectFrame" name="projectFrame" scrolling="no" allowtransparency="true"></iframe> 
            </div> 
            <div class="modal-footer"> 
                      <button type="button" runat="server" onserverclick="btnClose_Click"  class="btn" aria-hidden="true"> 
                    Close</button> 
            </div> 
        </div> 

为什么它在除 IE 之外的任何地方都可以工作?我以为 javascript 无处不在?

谢谢

它说:

SCRIPT5007: Unable to get property 'document' of undefined or null reference  
kezcommon.js, line 62 character 5 
 
if (frames.contentWindow.document.body) { 

请您参考如下方法:

window.frames是 native DOM 集合,您正在分配一个具有相同名称的节点列表。 IE 以某种方式搞砸了这两个 frames , 它们不相似。

autoresize_frames()似乎使用 window.frames集合而不是 frames节点列表(在 autoResizeFrames() 中定义)。但是,访问 iframe 时通过这个集合,没有 contentWindow或 contentDocument在 IE 中可用,因为 window.frames包含实际 window iframe 中的对象

您需要直接访问 frames[i].document或 frames[i]这是 window在iframe .

快速修复是使用其他名称而不是 frames对于节点列表。