2017-02-20 7 views
0

Я пытаюсь получить все элементы HTML-страницы с определенным идентификатором. Это отлично работает в Safari, Chrome и Firefox.«Объект JScript ожидается» в IE8

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 или выбрать их отдельно.

Кроме того, цикл for/in предназначен для обработки всех свойств объекта, у вас есть массив, который вы хотите пропустить. Код должен выглядеть так:

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))); 
 
});