【问题标题】:Stop iframe loading gives undefined error停止 iframe 加载会给出未定义的错误
【发布时间】:2014-06-24 11:11:54
【问题描述】:

我有一个脚本,将几个 iframe(例如 8 个)附加到正文中:

image_iframe[sequential number 1-8] = $('<iframe />');
image_iframe[sequential number 1-8].appendTo('body');

当我尝试阻止特定 iframe 中的图像加载时:

if(window.stop !== undefined) {
      image_iframe[2].contentWindow.stop();
}
else if(document.execCommand !== undefined) {
      image_iframe[2].contentDocument.execCommand("Stop", false);
}

我收到以下错误:

未捕获的类型错误:无法读取未定义的属性“停止”

我该如何解决这个问题?


Here's a jsfiddle 带有原始代码。只需单击“下一步”按钮几次即可重现该错误。它基于来自this questionthis jsfiddle,效果很好。

我做了一些搜索,发现了这两个问题,但不确定是否是同一个问题:

  1. I get an undefine error in iframe
  2. how to correctly stop the transport inside an iframe

【问题讨论】:

    标签: javascript jquery iframe


    【解决方案1】:

    image_iframe[index] 是一个 jQuery 元素。要获取contentWindow,您需要先获取 iFrame DOM 元素。建议改变

    // from
    image_iframe[this - 3].contentWindow.stop(); 
    
    // to
    image_iframe[this - 3].get(0).contentWindow.stop();
    

    【讨论】:

    • 我做了更改,虽然它确实消除了错误,但它仍然没有运行。图像仍在加载中(检查网络选项卡)。用 window.stop() 替换 frame.contentWindow;工作正常,但它会停止所有图像,而不仅仅是指定的 iframe。
    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 2014-12-26
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多