无法按预期访问子 iframe 内容
fail to access child iframe content as expected
只是尝试了一个关于 标记用法的非常简单的案例,而结果让我感到困惑。希望有人能帮助我。我在等,谢谢。
基本上,我有两个简单的页面,top.html和iframe.html,它们是同一个域, 而 iframe.html 是 iframe.html 中的 "src" 标签top.html.
当我尝试访问 top.html 中的 内容时,虽然结果很奇怪,但所有元素都是空白。
来自 top.html 的控制台日志非常奇怪,来自 iframe.html 的控制台日志是预期的。
// 加载页面后来自 top.html 的控制台日志
outer log: NodeList[head, body] --- top.html (line 6)
outer log: 2 --- top.html (line 7)
outer log: "" --- top.html (line 8)
// 单击
中的按钮后来自 ifram.html 的控制台日志
inner log: NodeList[head, <TextNode textContent="\n">, body] --- iframe.html (line 6)
inner log: 3 --- iframe.html (line 7)
inner log: "Child iFrame" --- iframe.html (line 8)
// top.html
<html>
<body>
<iframe id="fid" src="/iframe.html"></iframe>
<script type="text/javascript">
var i = document.getElementById("fid");
console.log("outer log: %o", i.contentDocument.documentElement.childNodes);
console.log("outer log: %o", i.contentDocument.documentElement.childNodes.length);
console.log("outer log: %o", i.contentDocument.documentElement.childNodes[1].innerHTML);
</script>
</body>
</html>
// iframe.html
<html>
<head>
<script type="text/javascript">
function show () {
var x = window.top.document.getElementById("fid");
console.log("inner log: %o", x.contentDocument.documentElement.childNodes);
console.log("inner log: %o", x.contentDocument.documentElement.childNodes.length);
console.log("inner log: %o", x.contentDocument.documentElement.childNodes[2].childNodes[1].innerHTML);
}
</script>
</head>
<body>
<p>Child iFrame</p>
<input type="button" onclick="show()" value="Show Button" />
</body>
</html>
谢谢
添加测试到window.onload,结果符合预期。
只是尝试了一个关于 标记用法的非常简单的案例,而结果让我感到困惑。希望有人能帮助我。我在等,谢谢。
基本上,我有两个简单的页面,top.html和iframe.html,它们是同一个域, 而 iframe.html 是 iframe.html 中的 "src" 标签top.html.
当我尝试访问 top.html 中的 内容时,虽然结果很奇怪,但所有元素都是空白。
来自 top.html 的控制台日志非常奇怪,来自 iframe.html 的控制台日志是预期的。
// 加载页面后来自 top.html 的控制台日志
outer log: NodeList[head, body] --- top.html (line 6)
outer log: 2 --- top.html (line 7)
outer log: "" --- top.html (line 8)
// 单击
中的按钮后来自 ifram.html 的控制台日志inner log: NodeList[head, <TextNode textContent="\n">, body] --- iframe.html (line 6)
inner log: 3 --- iframe.html (line 7)
inner log: "Child iFrame" --- iframe.html (line 8)
// top.html
<html>
<body>
<iframe id="fid" src="/iframe.html"></iframe>
<script type="text/javascript">
var i = document.getElementById("fid");
console.log("outer log: %o", i.contentDocument.documentElement.childNodes);
console.log("outer log: %o", i.contentDocument.documentElement.childNodes.length);
console.log("outer log: %o", i.contentDocument.documentElement.childNodes[1].innerHTML);
</script>
</body>
</html>
// iframe.html
<html>
<head>
<script type="text/javascript">
function show () {
var x = window.top.document.getElementById("fid");
console.log("inner log: %o", x.contentDocument.documentElement.childNodes);
console.log("inner log: %o", x.contentDocument.documentElement.childNodes.length);
console.log("inner log: %o", x.contentDocument.documentElement.childNodes[2].childNodes[1].innerHTML);
}
</script>
</head>
<body>
<p>Child iFrame</p>
<input type="button" onclick="show()" value="Show Button" />
</body>
</html>
谢谢
添加测试到window.onload,结果符合预期。