2011-03-02 48 views
0

我有这个疯狂的JavaScript问题与IE7和IE8。下面的函数表达式加载一些图像。而已。非常简单。当函数被直接调用,即testmypatience()它会像它应该那样工作,但如果我从超时内调用它,它将不会加载图像。该函数被调用,但图像不会加载。当由jQuery animate回调调用时,它也无法工作。功能不工作,因为它应该在IE7和IE8从setTimeout调用

我试过了一切,我无法得到它的工作,所以你的帮助将是最感激。

var testmypatience = function($active){ 
       var arr = ['images/site/products-medium/m_cordial_pomegranateelderflower.png', 'images/site/products-medium/m_cordial_spicedberry.png', 'images/site/products-medium/m_cordial_strawberryelderflower.png', 'images/site/products-medium/m_750presse_coxsapple.png', 'images/site/products-medium/m_party_appleandelderflower.png', 'images/site/products-medium/m_squeezy_blackcurrentandapple.png', 'images/site/products-medium/m_270presse_cranberryandorange.png', 'images/site/products-medium/m_270presse_elderflower.png', 'images/site/products-medium/m_270presse_gingerlemongrass.png']; 
       for (i = 0; i < arr.length; i++) { 
        var img, len; 
        img = new Image(); 
        img.src = arr[i]; 

        img.onload = function(){ 
         console.log('done ' + this.src); 
        } 
       } 
      } 

//this works 
testmypatience() 

//None of these work 
    setTimeout(function(){ 
     testmypatience() 
    }, 400) 

    setTimeout(testmypatience, 400) 

    jQuery('elm').animate({left:'1000px'}, 
    { 
     duration:200, 
     complete: testmypatience 
    }); 
+0

我只是想补充一点,当我说“没有这些工作,”上面我的意思是他们做调用函数但IE7和IE8不会触发onload事件,也不会加载图像。 – screenm0nkey 2011-03-02 11:41:48

+2

也许你必须把1000px放在''''内。如果需要,请在Chrome中运行。 – pimvdb 2011-03-02 11:42:16

+0

@pimvdb - 这只是一个例子。我只是表明它是从Jquery方法中调用的。从回调中调用它不起作用的地步。我会从代码中删除这一点,因为它很明显造成混乱 – screenm0nkey 2011-03-02 11:57:11

回答

2

您不需要像解决方案中那样复杂。只需交换srconload

错误:

img = new Image(); 
img.src = arr[i]; 
img.onload = function(){ ... } 

右:

img = new Image(); 
img.onload = function(){ ... } 
img.src = arr[i]; 
+0

谢谢你。这是正确的答案。不完全确定为什么用户会在+1上写下gonchuki的帖子,因为它与我的问题有关是不正确的,尽管我很欣赏他的回复,因为我会花时间回复我的问题。 Stackoverflow是坚果。 – screenm0nkey 2011-03-24 11:48:04

+0

+1 - 只需再次引用您的答案。再次感谢你;) – screenm0nkey 2011-07-14 10:43:41

2

除非您为此安装了浏览器扩展/插件,否则IE7和8没有控制台。注释与console.log电话线或使用类似FireBug Lite

另外,作为pimvdb称,1000px应在报价(不带引号它打破了所有的浏览器,它是无效的JavaScript语法)。

+0

IE9已经构建了一个日志控制台,它还允许您选择浏览器模式,以便您可以在ie7和8中看到输出。它不是关于日志记录。我所做的一点是,当从超时调用函数时,永远不会调用加载方法。 – screenm0nkey 2011-03-02 11:56:46

0

我已经整理了它。我必须先将一个空的图像对象插入到dom中,然后给它一个src属性,然后才能在IE7和8中触发onload事件。如果我有点含糊,请随时提出任何问题,而且我将回答你所有的问题。

$(function(){ 
       var testmypatience = function($active){ 
        var arr = ['images/site/products-medium/m_cordial_pomegranateelderflower.png', 'images/site/products-medium/m_cordial_spicedberry.png', 'images/site/products-medium/m_cordial_strawberryelderflower.png', 'images/site/products-medium/m_750presse_coxsapple.png', 'images/site/products-medium/m_party_appleandelderflower.png', 'images/site/products-medium/m_squeezy_blackcurrentandapple.png', 'images/site/products-medium/m_270presse_cranberryandorange.png', 'images/site/products-medium/m_270presse_elderflower.png', 'images/site/products-medium/m_270presse_gingerlemongrass.png']; 
        for (i = 0; i < arr.length; i++) { 
         var img, len; 
         img = new Image(); 
         $(img).prependTo('body'); 

         img.onload = function(){ 
          console.log('done ' + this.src); 
         } 
        } 

        $('img').each(function(i, elm){ 
         elm.src = arr[i]; 
        }) 
       } 

       setTimeout(function(){ 
        testmypatience() 
       }, 400) 
      })