iframe剂量工作

时间:2016-10-29 17:34:09

标签: javascript arrays iframe parent-child

我有代码:

<iframe src="in1.html" width="400" height="300" id="f1">
<p>Your browser does not support iframes.</p>
</iframe>

<script>
var childFrame = document.getElementById("f1").contentWindow;
childFrame.document.body.style.backgroundColor = "yellow"
</script>

当我加载页面黄色显示,但然后加载白色背景。为什么呢?

1 个答案:

答案 0 :(得分:1)

您必须等待iframe加载....

iframe src="in1.html" width="400" height="300" id="f1">
<p>Your browser does not support iframes.</p>
</iframe>

<script>
    var childFrame = document.getElementById("f1");

    childFrame.onload = function() {
        var cWindow = childFrame.contentWindow;
        cWindow.document.body.style.backgroundColor = "yellow";
    }
</script>