我在外部.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);
}
答案 0 :(得分:6)
IE < 9 doesn’t support getElementsByClassName
.由于chooseImage
函数依赖于它,引擎会抛出错误而不是继续运行代码。