IE中的jQuery load()回调失败

jQuery load() callback failure in IE

本文关键字:回调 失败 load 中的 jQuery IE      更新时间:2023-09-26

大家好,在发布这篇文章之前,我已经去了很多地方,试图解决这个问题,但都做不到。这是我遇到的最接近的东西。

我正在尝试动态创建一个iframe,并将内容加载到它的src中,然后使用回调对其进行处理

我有这样的东西:

var iframe = $('#helloworld');
if (!iframe.exists()) {
    iframe = $('<iframe/>', { 
        id : 'helloworld',
        src : "about:blank"
    }).css({
        width : "100%",
        height : "100%"
    });
}

iframe.load(options.src, function() {
    div.append(this);
    $(this).find('body').append(box);
    box.dialog({
        close: function( event, ui ) {
            iframe.load("about:blank");
            div.hide();
        }
    });
});

与许多其他人的问题类似,它在回调时失败了。从我在其他帖子中看到的情况来看,这是由于无效的html返回到iframe,然而,我已经检查过了,我的html是有效的。

这就是我为iframe返回的内容,正如您所看到的,为了测试目的,它已经被剥离到了最低限度。

<!DOCTYPE html>
<html dir="ltr">
    <head>
        <title>Testing</title>
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
        <style type="text/css">
        </style>
    </head>
    <body></body>
</html>

在IE中,jQuery正在消亡@line 5951:

append: function() {
    return this.domManip(arguments, true, function( elem ) {
        if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
            this.appendChild( elem );
        }
    });
},

SCRIPT65535:对方法或属性访问的意外调用。jquery-1.9.1.js,第5951行,字符5

其中this应该是我的iframe,它没有appendChild方法。

有什么提示可以让我进一步解决这个问题吗?

不能像那样对iframe使用load()。如果您想知道内容何时加载到iframe中,请使用onload事件并设置src属性。

iframe.on("load", function() {
    //code here
});
iframe.attr("src", options.src);

加载内容时,您将获得回调。

现在要使用iframe中的内容,您需要使用contents()

iframe.contents().find("body")...

据我所知,.load将HTML加载到指定的节点中。

所以iframe.load(options.src, /* */);

将加载options.src的值作为iframe的HTML主体。我怀疑你是否想要那样。

我想你想要:

iframe.attr("src", options.src);
// or
iframe.attr("src", "about:blank");