【问题标题】:Can't write to iframe in FF or IE, only Chrome无法在 FF 或 IE 中写入 iframe,只有 Chrome
【发布时间】:2013-05-30 04:49:49
【问题描述】:

如果你在 CHrome 中运行这个 jsfiddle,它会工作,但在 IE 和 FF 上它会失败:http://jsfiddle.net/LbFPu/2/

我想这与每个引擎如何写入 DOM 有关吗?有什么办法可以等待以前的内容写完吗?例如:

function firstMe(){
    $('body').append('<div id="first"></div>');
    // many other new elements appended
}

function thenMe(){
    $('#first').append('etc');
    // many of the recently appended elements being referenced
}

当按顺序调用时,我想它会在 Chrome 中正常工作,但在 IE 和 FF 中则不行。从 jsfiddle 结果来看。

以前有人处理过这个问题吗?

【问题讨论】:

  • 在fiddle上,在带有FireBug的FF中,在JS的第一行和单步设置断点,输出就会在那里。

标签: javascript jquery


【解决方案1】:

在 FF 中,如果您在小提琴的最后一行周围添加 setTimeout(),它会起作用:

$('body').append('<iframe id="upload"></iframe>');
var form = $('<form enctype="multipart/form-data" id="image_upload" action="index.php">' + 
    '<input type="file" accept="image/*" multiple name="img[]" id="image" />' + '<br>' + 
    '<input type="submit" value="Upload images" class="upload" />' + '</form>');
setTimeout(function () {
    $('#upload').contents().find('body').append(form);
}, 100);

【讨论】:

    【解决方案2】:

    这只是因为您正在尝试写入尚未加载的 iframe,似乎 chrome 更快:

    $('body').append('<iframe id="upload"></iframe>');
    var form = $('<form enctype="multipart/form-data" id="image_upload" action="index.php">'
         + '<input type="file" accept="image/*" multiple name="img[]" id="image" />'
         + '<br>'
         + '<input type="submit" value="Upload images" class="upload" />'
         + '</form>');
    
    $('#upload').on('load', function() {
        $(this).contents().find('body').append(form);
    });
    

    FIDDLE

    【讨论】:

    • 完美,奇怪的是有时会闪烁,我可以看到插入的元素半秒钟,然后iframe会变成空白。
    猜你喜欢
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    相关资源
    最近更新 更多