具有以下代码:
$(function() {
$('#ifrm').load(function() {
try {
window.document.domain = "sub_domain.domain";
document.getElementById("ifrm"); // 1
console.log("bla");
document.getElementById("ifrm").id = "new_ifrm"; // 2
window.frames[0].location = "http://server_2.sub_domain.domain/.../iframe_2.html";
}
catch (e) {
console.log("exception!");
alert("Error: " + e);
}
});
});
...
<body>
<iframe src="http://server_2.sub_domain.domain/.../iframe_1.html" id="ifrm" style="height: 100px"></iframe>
</body>
1.)在#2行中,我要更改框架ID,在下一行中,我现在将具有NEW ID的新内容加载到框架中。输出如下:
bla
bla
exception! (TypeError: document.getElementById(...) is null)
问题是,如果帧ID现在不是“ ifrm”而是“ new_ifrm”,为什么更改id后再次触发“ load”事件?
2.)如果第1行导致异常(document.getElementById(...)为null),为什么输出“ bla”?异常不应立即将执行定向到“ catch”块,而忽略异常行(#1)下方的任何内容。
问题是,如果帧ID现在不是“ ifrm”而是“ new_ifrm”,为什么更改id后再次触发“ load”事件?
事件处理程序绑定到iframe本身。
该ID仅在您致电时使用getElementById
。
如果第1行导致异常
没有。
调用返回null
值的函数就可以了。
document.getElementById("ifrm").id = "new_ifrm";
当您尝试在null
值上创建属性时,将引发异常。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句