2010-09-19 16 views
15

IE7 e IE8 non sono avermi permesso di Splice mio array (Safari, Chrome, Firefox lavoro):Array giunzione genera un errore in IE

 lzaCreateAd1.weatherArray = new Array(); 
     var jWeatherIcon = $('.weatherIcon'); 

     jWeatherIcon.bind('click', function(){ 
      var targetID = $(this).attr('id') + 'Box', 
      idVal = targetID.substr(5,1); 

      var jTargetBox = $('#'+targetID); 

      if (jTargetBox.hasClass('inactive')) { 
       jTargetBox.removeClass('inactive').addClass('active'); 
       lzaCreateAd1.weatherArray.push(idVal); 
      } else if (jTargetBox.hasClass('active')) { 
       jTargetBox.removeClass('active').addClass('inactive'); 
       lzaCreateAd1.weatherArray.splice(lzaCreateAd1.weatherArray.indexOf(idVal),1); 
      } 
     }); 

IE getta il seguente errore: "L'oggetto non supporta questa proprietà o metodo" per questa linea:

lzaCreateAd1.weatherArray.splice(lzaCreateAd1.weatherArray.indexOf(idVal),1);

Tutte le idee? O altri modi per rimuovere un elemento dell'array in base al valore? Grazie in anticipo!

risposta

29

Array.indexOf non è supportato da Internet Explorer prima della versione 9. È possibile utilizzare la funzione di utilità $.inArray di jQuery o qualsiasi altro shim/polyfill desiderato.

lzaCreateAd1.weatherArray.splice($.inArray(idVal, lzaCreateAd1.weatherArray) ,1); 

See: http://api.jquery.com/jQuery.inArray/

+0

Grazie Yi Jiang! – Kyle

+9

Eppure UN'ALTRA ragione per cui IE è la rovina di molte esistenze di un web-devloper. Grazie! – exoboy

Problemi correlati