我的 HTML 页面中有两级父子 iframe 层次结构。我想在其子文档中获取父窗口文档的对象以进行一些操作。我主要使用谷歌浏览器。
parent.document 在 Google Chrome 中给出“未定义”,而在 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
提前致谢。