JavaScript getElementById(...) 为 null 或不是对象 IE

发布于 2024-09-07 19:05:15 字数 1301 浏览 10 评论 0原文

对于 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(...) 为 null 或不是对象”。有人可以建议我应该更改什么才能使其跨浏览器工作吗?

非常感谢, 阿里

This must be something very simple for the JavaScript experts out there. In the following code, I am trying to open an iframe to the full height of browser window.

    <%@ 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>

It works in Mozilla but in IE (only tested in IE8) it gives the error: 'document.getElementById(...) is null or not an object'. Can anybody suggest what should I change to make it work cross browser?

Many thanks,
Ali

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

书信已泛黄 2024-09-14 19:05:16

尝试

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。

Try wrapping

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

Within:

window.onload = function(){

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

The iframe doesn't exist in the DOM until the page/Dom has loaded.

UPDATE:

Didn't see part of the code you posted.

You could keep onload = "resizeIframe" within the element itself and have the JavaScript in the head as:

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


 window.onresize = resizeIframe;

That way you wouldn't need any JavaScript running after the element.

○闲身 2024-09-14 19:05:16
  1. alert(document.getElementById('abcId').value);
    
  2. alert(document.formName.elements['abcName'].value);
    

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

  1. alert(document.getElementById('abcId').value);
    
  2. alert(document.formName.elements['abcName'].value);
    

I expirienced the same issue i tried the 2nd one it worked.

Smile简单爱 2024-09-14 19:05:16

您需要

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

在文档准备好之前执行此操作,以便可以找到 iframe。在 onload 或 document-ready 函数中或在 iframe 之后的单独 javascript 块中调用此函数。

you do

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

before the document is ready so the iframe can be found. call this in the onload- or document-ready-function or in a seperate javascript-block after the iframe.

给不了的爱 2024-09-14 19:05:16

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

我更喜欢替换 < code>window.onresize = resizeIframe; by:

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

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

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

编辑:

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

If you have used onload"resizeIframe" in the iframe tag, you don't need document.getElementById('documentFrame2').onload = resizeIframe; anymore

I prefer relacing window.onresize = resizeIframe; by:

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

Sometimes the window will be resized before the iframe is ready for operation, and that would cause errors.

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

edited:

window.onload = function() {
  resizeIframe();
  window.onresize = resizeIframe;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文