JS的背景图像样式在ie9中不起作用
Background-image style with JS not working in ie9
我正在一个网站上工作,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);
slideshow
是undefined
,而undefined
不是setTimeout
的有效参数。
用匿名函数包装调用:
var timer = setTimeout(function(){slideshow();}, 8000);
匿名函数将是setTimeout
的有效参数,该函数的内容将在8秒后(当slideshow
被定义为函数时)进行评估
相关文章:
- Ajax模式下的Jquery Select2 V4在IE9中不起作用
- setAttribute和video.src用于更改IE9中不起作用的视频标记源
- JS代码使IE9和Safari冻结,在Opera中不起作用
- IE9+IE10 AJAX调用不起作用
- Angularjs 跨源资源共享 (CORS) 发布或放置方法在 IE8 和 IE9 中不起作用
- window.onload和.click()在ie9中不起作用
- `ie9`-contenteditable false在父级可编辑时不起作用
- 点击按钮dons'它在IE9中不起作用,但没有错误消息(它在chrome中起作用)
- 在IE9中javascript不起作用,但在打开控制台后它就起作用了
- 禁用编辑和删除按钮在IE9中不起作用,但在IE11中起作用
- jQuery ajax 请求与承诺在IE9中不起作用
- 选择的选项在IE9中不起作用
- CSS 按钮背景图像在 IE9 中不起作用
- d3js 加载静态 svg 在 IE9 中不起作用(responseXML = null)
- 由于覆盖(仅限 IE9),单击 事件不起作用
- 在IE9的新窗口中打开pdf不起作用
- 在文本区域中将
替换为 在 IE9 中不起作用 - 模态在IE9中不起作用 - 在打开开发人员工具时,它可以正常工作
- Flexislider在IE9中不起作用
- webshims FileReader 在 IE9 中不起作用