基本上,我有一个iframe嵌入在页面中,并iframe有一些我需要从父页面调用的JavaScript例程.
现在相反的情况非常简单,因为你只需要打电话parent.functionName(),但不幸的是,我需要完全相反.
请注意,我的问题是不会改变的源URL的iframe,但是调用在定义的函数iframe.
我有这个HTML代码:
<html>
<head>
<script type="text/javascript">
function GetDoc(x)
{
return x.document ||
x.contentDocument ||
x.contentWindow.document;
}
function DoStuff()
{
var fr = document.all["myframe"];
while(fr.ariaBusy) { }
var doc = GetDoc(fr);
if (doc == document)
alert("Bad");
else
alert("Good");
}
</script>
</head>
<body>
<iframe id="myframe" src="http://example.com" width="100%" height="100%" onload="DoStuff()"></iframe>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
问题是我收到消息"Bad".这意味着iframe的文档没有正确获取,GetDoc函数返回的实际内容是父文档.
如果你告诉我哪里弄错了,我会很感激.(我希望在IFrame中托管文档.)
谢谢.
我想访问和修改iframe内容.当我这样做的时候,Chrome 16和Firefox 9会返回"无法读取未定义的属性'文档".我做错了什么?
<iframe id="frame"></iframe>
Run Code Online (Sandbox Code Playgroud)
...
var myIFrame = document.getElementById("frame");
var content = myIFrame.contentDocument.document.body.innerHTML;
alert(content);
Run Code Online (Sandbox Code Playgroud)