开发者

Array.indexOf throws errors in some IE versions

IE7 and IE8 are not letting me splice my array (Safari, Chrome, Firefox work):

        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 throws the following error: "Object doesn't support this property or method" for this line:

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

Any ideas? Or other ways to remove an array item by value? Thanks in advance!


Array.indexOf is not supported by Internet Explorer before version 9. You can use the jQuery's $.inArray utility function, or any other shim/polyfill you want instead.

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜