我的HTML页面中有两级父 - 子iframe层次结构。我想在其子文档中获取父窗口文档的对象以进行一些操作。我主要与Google Chrome合作。parent.document在Chrome中未定义
parent.document在Google Chrome中给出'undefined',而在Mozilla中它可以正常工作。有什么收获?
仅供参考,请找三个文件证明问题的内容下方,
第一个文件: 'one.html'
<html>
<head>
</head>
<body>
<input type="hidden" id="one_1" name="one_1" />
<iframe id="one" name="one" src="two.html">
</body>
</html>
第二个文件: 'two.html'
<html>
<head>
</head>
<body>
<input type="hidden" id="two_1" name="two_1" />
<iframe id="two" name="two" src="three.html">
</body>
</html>
第三个文件: 'three.html'
<html>
<head>
<script type="text/javascript">
function callme() {
alert(parent.document)
alert(top.document)
}
</script>
</head>
<body>
<input type="hidden" id="three_1" name="three_1" />
<button id="click" name="click" onclick="return callme()">Click Me</button>
</body>
</html>
假设“one.html”打开谷歌浏览器,当我点击“点击我”按钮,两个连续的警报框将出现与“未定义”值。当我在Mozilla中打开'one.html'时,会出现两个'objectHTMLDocument'值的警告框。
请找到控制台消息之下,而点击“点击我”按钮,提前
Unsafe JavaScript attempt to access frame with URL file:///C:/Users/user/Desktop/two.html from frame with URL file:///C:/Users/user/Desktop/three.html. Domains, protocols and ports must match.
three.html:6
callme three.html:6
onclick three.html:13
Unsafe JavaScript attempt to access frame with URL file:///C:/Users/user/Desktop/one.html from frame with URL file:///C:/Users/user/Desktop/three.html. Domains, protocols and ports must match.
three.html:7
callme three.html:7
onclick three.html:13
感谢。
这个问题是['contentDocument'](https://developer.mozilla.org/en-US/docs/HTML/Element/iframe#Scripting)。 – bfavaretto 2013-03-26 20:08:01
@bfavaretto刚刚检查过contentDocument,仍然没有收获:( – dsharma4u29 2013-03-26 20:09:39
您能否显示您的代码? – bfavaretto 2013-03-26 20:10:45