JS的背景图像样式在ie9中不起作用

Background-image style with JS not working in ie9

本文关键字:ie9 不起作用 样式 背景 图像 JS      更新时间:2023-09-26

我正在一个网站上工作,IE9中的滑块出现了一些问题。我所做的是,我制作了一个div,根据其中的img标记,每隔几秒钟就会更改一次背景图像。

你可以在这里的功能中看到它:http://diakondk.quine.pil.dk/

它在大多数浏览器中都能发挥神奇的作用,但我似乎不明白为什么它在IE9中不起作用,或者由于我没有JS大师,我将如何使它发挥作用。

有什么建议吗?

var slideshow = (function () {
var images = $('.img-slider-wrapper img').map(function() { 
                  return this.src; 
             }).get()
             for(i = 0; i < images.length; i++) {
               $(".selector").append('<div class="dot" data-image="'+i+'" id="dot'+i+'"></div>');
             }
var index = images.length - 1;
var dot = "#dot"+index;
var timer = setTimeout(slideshow, 8000);
  $('.selector .dot').click(function() {
     index = this.dataset.image;
     $(".img-slider-wrapper").css('background-image', 'url(' + images[index] + ')');
     $(".dot.active").removeClass("active");
     dot = "#dot"+index;
     $(dot).addClass("active");
     clearTimeout(timer);
     timer = setTimeout(slideshow, 8000);
  });
  return function () {
    index = (index + 1) % images.length;
    $(".img-slider-wrapper").css('background-image', 'url(' + images[index] + ')');
    $(".dot.active").removeClass("active");
    dot = "#dot"+index;
    $(dot).addClass("active");
    clearTimeout(timer);
    timer = setTimeout(slideshow, 8000);
  }
 }());
 $(document).ready(slideshow);

您对setTimeout的调用在任何浏览器中都会失败,但在IE9中会出现异常(停止进一步的脚本执行)。

这只是时间问题。在你打电话给的那一刻

var timer = setTimeout(slideshow, 8000);

slideshowundefined,而undefined不是setTimeout的有效参数。

用匿名函数包装调用:

var timer = setTimeout(function(){slideshow();}, 8000); 

匿名函数将是setTimeout的有效参数,该函数的内容将在8秒后(当slideshow被定义为函数时)进行评估