我在应用程序的弹出窗口中有一个ajax风格的表单。我使用一个带有目标iframe的表单,然后在iframe.load()函数中,我获取内容并将其显示回弹出窗口中。
这适用于IE9、Chrome、Firefox:
$("iframe[name=addpart-iframe]").load(function () {
//FF and IE fire the load when the popup first load, chrome does not. This kips the first onload where no submit has happened yet
if (firstLoad == true) {
firstLoad = false;
return;
}
var response = this.contentDocument.body.innerHTML;
$("#AddForm").html(response);
});
除IE7外,这项功能非常有效。当我看到this.contentDocument.body时,调试器会说body不是一个有效的属性。我查看了外部HTML,此时我的iframe也是空的。不知道为什么!
contentDocument
属性引用iframe内的文档元素(这相当于contentWindow.document
),但IE8之前的Internet Explorer版本不支持该属性。
对于8之前的早期版本的IE,可以使用contentWindow
属性。
var response;
if (this.contentDocument) {
response = this.contentDocument.body.innerHTML;
} else {
response = this.contentWindow.document.body.innerHTML;
}