JavaScript getElementById(...)为null或不是对象IE [英] JavaScript getElementById(...) is null or not an object IE

查看:80
本文介绍了JavaScript getElementById(...)为null或不是对象IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那里的JavaScript专家来说,这必须非常简单.在以下代码中,我试图将iframe打开到浏览器窗口的整个高度.

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>

它可以在Mozilla中工作,但在IE中(仅在IE8中经过测试)会出现错误:"document.getElementById(...)为null或不是对象".有人可以建议我做些什么更改才能使其在跨浏览器中正常工作吗?

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?

非常感谢, 阿里

推荐答案

您要做的

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

在文档准备就绪之前可以找到iframe.在iframe之后,可以在onload或document-ready函数中或单独的javascript块中调用此函数.

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.

这篇关于JavaScript getElementById(...)为null或不是对象IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆