我试图在以下代码中访问iframe的contentDocument和contentWindow.但他们都是空的.
var iframe = document.createElement('iframe');
this.get__domElement$i().appendChild(iframe);
if (iframe.contentDocument) {
iframe.contentDocument.write("Hello");
}
else if (iframe.contentWindow) {
iframe.contentWindow.document.body.innerHTML = "Hello";
}
Run Code Online (Sandbox Code Playgroud)
有人能告诉我这有什么不对吗?谢谢
当我做Document.Body.AppendChild(iframe)时,contentDocument和contentWindow都是非null.
当我将iframe附加到div时,有人能告诉我什么是错的吗?
非常感谢.
我知道这有点晚了,但我正在研究这个并偶然发现了你的问题:
我遇到了与您相同的错误,因为当我尝试附加 iframe 时,我尝试附加 iframe 的 div 尚未加载。如果 div 已加载,您的代码可以正常工作:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id='test' >
</div>
<script>
var iframe = document.createElement('iframe');
var myDiv = document.getElementById('test');
myDiv.appendChild(iframe);
if (iframe.contentDocument) {
iframe.contentDocument.write("Hello");
}
else if (iframe.contentWindow) {
iframe.contentWindow.document.body.innerHTML = "Hello";
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是一个包含此工作代码的jsFiddle ,另一个包含给出错误中的标签TypeError: Cannot call method 'appendChild' of null。
| 归档时间: |
|
| 查看次数: |
7569 次 |
| 最近记录: |