【问题标题】:JavaScript getElementById(...) is null or not an object IEJavaScript getElementById(...) 为 null 或不是对象 IE
【发布时间】:2010-06-30 08:25:13
【问题描述】:

对于 JavaScript 专家来说,这一定是一件非常简单的事情。在下面的代码中,我试图打开一个 iframe 到浏览器窗口的全高。

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
         <script language="javascript" type="text/javascript">
    function resizeIframe() {
    var height = document.documentElement.clientHeight;
    height -= document.getElementById('documentFrame2').offsetTop;
    height -= 20;
    document.getElementById('documentFrame2').style.height = height +"px";
 };
document.getElementById('documentFrame2').onload = resizeIframe;
window.onresize = resizeIframe;
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <iframe src="standard-tube-map.pdf" id="documentFrame2" width="100%" onload="resizeIframe();" ></iframe> 
    </div>
    </form>
</body>
</html>

它在 Mozilla 中有效,但在 IE 中(仅在 IE8 中测试)它给出错误:'document.getElementById(...) is null or not an object'。有人可以建议我应该更改什么以使其跨浏览器工作吗?

非常感谢, 阿里

【问题讨论】:

    标签: javascript internet-explorer getelementbyid


    【解决方案1】:

    尝试换行

    document.getElementById('documentFrame2').onload = resizeIframe;
    

    在:

    window.onload = function(){
    
        document.getElementById('documentFrame2').onload = resizeIframe;
        window.onresize = resizeIframe;
        resizeIframe();
    };
    

    在页面/Dom 加载之前,iframe 不存在于 DOM 中。

    更新

    没有看到您发布的部分代码。

    您可以将 onload = "resizeIframe" 保留在元素本身中,并将 JavaScript 放在头部:

    function resizeIframe() {
        var height = document.documentElement.clientHeight;
        height -= document.getElementById('documentFrame2').offsetTop;
        height -= 20;
        document.getElementById('documentFrame2').style.height = height +"px";
     }
    
    
     window.onresize = resizeIframe;
    

    这样你就不需要在元素之后运行任何 JavaScript。

    【讨论】:

    • 谢谢汤姆格罗尔。试了之后,状态栏没有报错,但是iframe的大小是默认的。
    • 非常感谢您的努力,但它仍然无法在 IE 中为我工作。当我调整窗口大小时,它只会变成全高。还有其他解决方法吗?再次感谢。
    • 在页面加载时,您可以让它运行 resizeIframe()。那将解决它。我已经编辑了我的答案,因为它更容易所以检查一下。
    【解决方案2】:
    1. alert(document.getElementById('abcId').value);
      
    2. alert(document.formName.elements['abcName'].value);
      

    我遇到了同样的问题,我尝试了第二个问题。

    【讨论】:

      【解决方案3】:

      你会的

      document.getElementById('documentFrame2').onload = resizeIframe;
      

      在文档准备好之前,可以找到 iframe。在 onload- 或 document-ready-function 或 iframe 之后的单独 javascript-block 中调用它。

      【讨论】:

      • 刚刚从 中删除了脚本并将其放在
      【解决方案4】:

      如果您在 iframe 标记中使用了onload"resizeIframe",则不再需要document.getElementById('documentFrame2').onload = resizeIframe;

      我更喜欢通过以下方式替换window.onresize = resizeIframe;

      window.onload = function() {
        window.onresize = resizeIframe;
      };
      

      有时窗口会在 iframe 准备好运行之前调整大小,这会导致错误。

      ==========================

      编辑:

      window.onload = function() {
        resizeIframe();
        window.onresize = resizeIframe;
      };
      

      【讨论】:

      • 谢谢彼得,但这只有在您调整窗口大小时才有效。
      猜你喜欢
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多