预期的对象 - IE8和IE9中的jQuery错误

时间:2013-04-06 17:19:51

标签: jquery google-maps-api-3

我使用的是Google Maps API v3,我遇到了IE8,9的问题。请注意,在其他浏览器中,它可以完美地工作,没有错误(在页面或控制台上)。

jQuery(document).ready(function () {

    var target = jQuery('.google-map'),
        desiredheight = target.parent().siblings('div').height();

    target.height(desiredheight);

});

jQuery(document).ready(function () {

    var target = jQuery('.google-map'),
        myLatlng, myOptions, map, infowindow, marker, mapStyle, styledMap;

    if (target.length > 0) {

        myLatlng = new google.maps.LatLng(target.attr('data-latitude'), target.attr('data-longitude'));

        myOptions = {
            zoom: parseInt(target.attr('data-zoom'), 10) || 6,
            center: myLatlng,
            html: target.attr('data-title'),
            mapTypeControl: false,
            popup: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(target[0], myOptions);

        mapStyle = [{
            stylers: [{
                saturation: -100
            }]
        }, {
            featureType: 'road',
            elementType: 'geometry',
            stylers: [{
                lightness: 100
            }, {
                visibility: 'simplified'
            }]
        }, {
            featureType: 'road',
            elementType: 'labels',
            stylers: [{
                visibility: 'off'
            }]
        }];

        if (mapStyle) {

            styledMap = new google.maps.StyledMapType(mapStyle, {
                name: 'InspireThemes'
            });
            map.mapTypes.set('map_style', styledMap);
            map.setMapTypeId('map_style');

        }

        infowindow = new google.maps.InfoWindow({
            content: target.attr('data-content')
        });

        marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: target.attr('data-title')
        });

        google.maps.event.addListener(marker, 'click', function () {

            infowindow.open(map, marker);

        });

    }

});

我在第72行收到错误报告,这是:

myLatlng = new google.maps.LatLng( target.attr( 'data-latitude' ), target.attr( 'data-longitude' ) );


现在,我当然尝试使用Google,并且所有问题都是由数组或对象末尾的额外逗号引起的,当然这在IE中不起作用。但即使我的问题确实有这样的问题,我也无法弄清楚我会在哪里得到额外的逗号。

我尝试了各种js工具但没有帮助。

2 个答案:

答案 0 :(得分:0)

老实说,我实际上并不熟悉它们的这部分API,也不知道你的整个用例,但是从你的其余代码中我认为问题是你没有指定哪个目标到从这里获取该属性:

myLatlng = new google.maps.LatLng( target.attr( 'data-latitude' ), target.attr( 'data-longitude' ) ); 

它认为它应该是:

myLatlng = new google.maps.LatLng( target[i].attr( 'data-latitude' ), target[i].attr( 'data-longitude' ) );

其中i是您从target获取该属性的对象的索引。


(数组中的第一个元素是jQuery元素)。 这也可以解释为什么你得到object expected错误,因为你在.attr上调用target,这是一个对象数组,但它本身不是一个对象。这可能有点令人困惑,因为jQuery方法,如.attr可以在jQuery数组上调用,但target不是jQuery数组,即使它是,你可能也不会得到你期望的结果。


<强>更新

这不是答案,但您可能还需要在parseInttarget.attr('data-latitude')上致电target.attr('data-longitude')

答案 1 :(得分:0)

通过上传到实时服务器解决了问题,localhost导致了问题。

感谢所有帮助过的人,我很感激!

相关问题