2015-01-08 186 views
-1

我有一個html調用這個js文件。 html和js在ie9和更高版本中工作正常,但由於某種原因,ie8不能使用此js文件,是否有可能html文件沒有加載文件,或者代碼有問題?JS代碼不能在IE8中工作?

function countTotalByClassName(klass){ 
total=0; 
var arr = document.getElementsByClassName(klass); 
for (var index = 0; index < arr.length; index++) { 
    // console.log(arr[index].checked); 
    if(arr[index].checked){ 
    total=total+1; 
    } 
}; 
return total; 
} // END countTotalByClassName() 

/* getGreatestChoice() 
* 
* Given an array of hashes, return the one with the highest number. 
* 
*/ 
function getGreatestChoice(arr){ 
res=""; 
size=0; 
for (var i = 0; i < arr.length; i++) { 
if (arr[i].count > size){ 
    res=arr[i].name; 
} 
} 
return res; 
} // END getGreatestChoice() 

function results(){ 
item1 = {}; 
item2 = {}; 
item3 = {}; 
item4 = {}; 

arr=[]; // array of items 

// BEGIN BUILD HASH ARRAY 
item1["name"]="<h1>Lorem1</h1><p>random text</p><img src='img/Hi.png' class='img-responsive'>"; 
item1["count"]=countTotalByClassName("A"); 
arr.push(item1); 
item2["name"]="<h1>Lorem2</h1><p>random text</p><img src='img/Hi.png' class='img-responsive'>"; 
item2["count"]=countTotalByClassName("B"); 
arr.push(item2); 
item3["name"] = "<h1>Lorem3</h1><p>random text</p><img src='img/Hi.png' class='img-responsive'>"; 
item3["count"] = countTotalByClassName("C"); 
arr.push(item3); 
item4["name"] = "<h1>Lorem4</h1><p>random text</p><img src='img/Hi.png' class='img-responsive'>"; 
item4["count"] = countTotalByClassName("D"); 
arr.push(item4); 
// END BUILD HASH ARRAY 

res=getGreatestChoice(arr); 

document.getElementById('result').innerHTML = res; 
document.getElementById('result').style.width = '100%'; 
document.getElementById('result').style.backgroundColor = '#F0F0F0'; 
document.getElementById("result").style.padding="5px 10px 10px 10px"; 

} // END results() 
+1

開發者控制檯是否輸出任何內容? – Nick

+4

定義「不工作」。任何錯誤記錄,或不是預期的結果等? –

+7

看起來像['getElementsByClassName'](https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByClassName#Browser_compatibility)不在IE8 –

回答

2

看來,方法getElementsByClassName is not supported in IE8。嘗試使用querySelectorAll,在IE8中正常工作。

相反的:

var arr = document.getElementsByClassName(klass); 

用途:

var arr = document.querySelectorAll('.' + klass); 

如果您還需要對IE6的支持,那麼你可以使用this polyfill

+2

假設你不介意將問題移到IE7 :) –

+1

你是對的,我添加了一個polyfill到我的答案支持IE6 – lante

+0

我不支持IE7或更低版​​本。謝謝蘭特,多數民衆贊成在罰款 – rob