IFRAME中的SRC属性随onload函数而变化

SRC attributes in IFRAME change with onload function

本文关键字:函数 变化 onload 中的 SRC 属性 IFRAME      更新时间:2023-09-26

如果我在IFRAME中有"src"属性,则更改为不执行"onload"函数。它只有在第一次调用时才有效。

JavaScript:

function dialog () {
    document.getElementById('result').src = "myurl";
}
function loading() {
    document.getElementById('loading').style.display = "none";
    document.getElementById('result').style.display = "block";
}

HTML:

<div id="loading"><img src="loading.gif"/></div>
<iframe style="display:none;" id="result" onload="loading();" src="myurl"/></iframe>

谢谢!

您的页面域和iframe域相同吗?如果它们不同,您可以使用CORS来访问iframe事件。


编辑

<script>
    function dialog() {
        document.getElementById('result').src = "http://jsfiddle.net";
    }
    function loading() {
        alert('Loading finished');
        document.getElementById('loading').style.display = "none";
        document.getElementById('result').style.display = "block";
    }
</script>
<div id="loading">Loading</div>
<iframe  style="display:none;" id="result" onload="loading();" src="http://jsfiddle.net"/>

它工作,现场演示--http://jsfiddle.net/xf2LgLsg/我相信你是在HTML之后写JS的,所以这可能是个问题。