首页 > 解决方案 > 在 IE 8 中有时无法访问对象

问题描述

我有一个带有两个子框架的框架集。

<frameset rows="241px,*" id="header_fr">
    <frame src="frame1.htm"/>
    <frame src="frame2.htm"/>
</frameset>

frame1.htm只用作标题,每次更改内容时都会动态更改其大小frame2.htm

我改变frame1.htm使用的大小parent.document.getElementsByTagName("frameset")[0].rows = new_size + ",*";

一切正常,但有时parent.document.getElementsByTagName("frameset")[0]不再可用,它为 null 或不是 object。这个问题的原因是什么?

标签: javascripthtmlinternet-explorerframesframeset

解决方案


你如何使用 JavaScript 函数?可能是 DOM 没有完全加载导致问题。您应该确保在对象完全加载后执行该函数。

我使用下面的代码进行测试,它在 IE 8 中运行良好,你可以检查一下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
    <frameset rows="241px,*" id="header_fr">
        <frame src="frame1.htm" />
        <frame src="frame2.htm" />
    </frameset>
</html>

框架1.htm

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body style="background-color:aquamarine">
    111
</body>
</html>

框架2.htm

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body style="background-color:burlywood">
    222
    <script>
        parent.document.getElementsByTagName("frameset")[0].rows = 40 + ",*";
    </script>
</body>
</html>

推荐阅读