Javascript iframe contentDocument和contentWindow为空

Javascript iframe contentDocument和contentWindow为空,javascript,iframe,Javascript,Iframe,我试图在下面的代码中访问iframe的contentDocument和contentWindow。但它们都是空的 var iframe = document.createElement('iframe'); this.get__domElement$i().appendChild(iframe); if (iframe.contentDocument) { iframe.contentDocument.write("Hello"); }

我试图在下面的代码中访问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";
     }
有人能告诉我这有什么问题吗?谢谢

当我执行Document.Body.AppendChild(iframe)时,contentDocument和contentWindow是非空的

有人能告诉我当我将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>

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=“你好”;
}
下面是一个使用此工作代码和中的标记的示例,给出了一个错误
TypeError:cannotcallmethod'appendChild'of null