2017-02-20 2 views
0

특정 ID가있는 HTML 페이지의 모든 요소를 ​​가져 오려고합니다. Safari, Chrome 및 Firefox에서 정상적으로 작동합니다. IE8에서 "JScript object expected"

var value_fields_value = []; 
 
    var value_fields_alert = []; 
 
    var Variables = []; 
 
    var e; 
 

 
    
 
    value_fields_value = Array.prototype.slice.call(document.querySelectorAll('[id^=value_]')); 
 
    for(var i in value_fields_value){ 
 
     Variables.push(new Element(value_fields_value[i], new Adresse(value_fields_value[i].id.toString().replace('value_', ''), null, null, null, null))); 
 
    }

이 너무 Internet Explorer에서 작동해야하지만 나는 오류 메시지 "예상 JScript의 객체"를 얻고있다

.

누구에게 아이디어가 있습니까? (jquery를 사용하지 않고)

감사합니다.

+0

[IE8이 querySelectorAll 지원하지 않습니다]의 사용 가능한 복제 (http://stackoverflow.com/questions/16920365/ie8-does-not-support- queryselectorall) –

답변

0

IE8과 역 호환되어야하는 경우 querySelectorAll을 사용할 수 없습니다. getElementsByTagName을 사용하거나 개별적으로 선택하는 것이 좋습니다.

또한 루프는 객체의 모든 속성을 반복하도록 설계되었으므로 루프하려는 배열이 있습니다. 코드는 다음과 같아야합니다

var value_fields_alert = []; 
 
var Variables = []; 
 
var e; 
 

 
// No need to pre-declare this to an empty array when you are just going 
 
// to initialize it to an array anyway 
 
var value_fields_value = Array.prototype.slice.call(document.querySelectorAll('[id^=value_]')); 
 

 
// You can loop through an array in many ways, but the most traditional and backwards compatible 
 
// is a simply for counting loop: 
 
for(var i = 0; i < value_fields_value.length; ++i){ 
 
    Variables.push(new Element(value_fields_value[i], new Adresse(value_fields_value[i].id.toString().replace('value_', ''), null, null, null, null))); 
 
} 
 

 
// Or, you can use the more modern approach: 
 

 
// The Array.prototype.forEach() method is for looping through array elements 
 
// It takes a function as an argument and that function will be executed for 
 
// each element in the array. That function will automatically be passed 3 arguments 
 
// that represent the element being iterated, the index of the element and the array itself 
 
value_fields_value.forEach(function(el, in, ar){ 
 
    Variables.push(new Element(el, new Adresse(el.id.toString().replace('value_', ''), null, null, null, null))); 
 
});