2

我有两个 html 文件。一个在 iframe 中包含另一个。iframe 对先前创建的元素执行 getElementById。这在 Chrome/Safari 中运行良好,但在 Firefox/IE8 中没有,我不明白为什么。

火狐返回:空

Safari 返回:[object HTMLDivElement]

索引.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>untitled</title>
</head>
<body>
    <iframe src="comment.html"></iframe>
</body>
</html>

评论.html

<div id="foobar"></div>
<script type="text/javascript">
    alert(document.getElementById('foobar'));
</script>
4

1 回答 1

4

您的评论 html 文件格式不正确。尝试使用:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>untitled</title>
</head>
<body>
<div id="foobar"></div>
<script type="text/javascript">
    alert(document.getElementById('foobar'));
</script>

</body>
</html>
于 2010-03-02T13:07:19.180 回答