2017-02-20 2 views
0

特定のIDを持つHTMLページのすべての要素を取得しようとしています。これはSafari、Chrome、Firefoxで正常に動作します。IE8で "JScriptオブジェクトが必要です"

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

の可能性のある重複(http://stackoverflow.com/questions/16920365/ie8-does-not-support- [IE8 querySelectorAllをサポートしていません]:コードは次のようにする必要があります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))); 
 
});

関連する問題