我在一个运行正常的外部 .js 文件中有一些 JavaScript(1.7.2)。但是在检查 IE 兼容模式以确保它与 IE7+ 兼容后,我遇到了一个问题。在页面加载时调用的函数 loader() 中,调用 chooseImage(img) 会阻止后续调用 timeImgChange()。如果我更改这两个调用的顺序,问题仍然存在,因为没有调用 chooseImage()。希望有人能帮忙,尼尔。
window.onload = loader;
function loader(){
var img = document.getElementById('selector_chicks');
chooseImage(img); //This function call works
timeImgChange(); //Not called. Works in IE9. Is called if chooseImage() is removed
}
function chooseImage(img){
document.getElementById('chooseimage').src = imageMap[img.title].path;
document.getElementById(img.id).style.background = "#A66B00";
document.getElementById('textselected').innerHTML = imageMap[img.title].text;
/*unselect the other divs*/
var selectors = document.getElementsByClassName('selector');
for (var i = 0; i < selectors.length; i++){
if (selectors[i].title != img.title){
selectors[i].style.background = "#261e1e";
}
}
}
function timeImgChange() { /*Change the image ever x milliseconds*/
var imgIdArray = ["selector_egg", "selector_chicks", "selector_poults"];
var n = 1;
setInterval(function(){
if (doImageCycle){
var img = document.getElementById(imgIdArray[n]);
chooseImage(img);
n++;
if (n > 2) {n = 0;}
}
}, 2000);
}