IE7/8 javascript“SCRIPT5007:无法获取属性值”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10430460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IE7/8 javascript "SCRIPT5007: Unable to get value of the property"
提问by alexleonard
I've got a site under development which uses Google Maps API V3 to load multiple location makers. Everything is working except in IE7/8 where my looping through an array fails due to the following error:
我有一个正在开发的网站,它使用 Google Maps API V3 来加载多个定位器。一切正常,除了在 IE7/8 中,由于以下错误,我的数组循环失败:
SCRIPT5007: Unable to get value of the property '5': object is null or undefined
I've looked through all similar questions on StackOverflow and can't find any solution.
我在 StackOverflow 上查看了所有类似的问题,但找不到任何解决方案。
So I'm trying to access data from an object array (I also tried a nested array but the same error occurs). My array is as below. I have also tried dropping the html object in case that was causing an issue, but no change - IE7/8 still can't get the object value.
所以我试图从一个对象数组访问数据(我也尝试了一个嵌套数组,但发生了同样的错误)。我的阵列如下。我也尝试删除 html 对象,以防导致问题,但没有改变 - IE7/8 仍然无法获取对象值。
var sites = [
{title: "Hermitage Medical Clinic", latitude: 53.3593139774, longitude: -6.40494071541, enumerator: 1, html: "<div class=\"find_a_surgeon_info_inner\"><h4 style=\"margin: 0; padding: 0;\">Hermitage Medical Clinic</h4><p>Old Lucan Road, Dublin 20</p><p><a href=\"http://devel.pixelapes.com/iaps.ie/location/hermitage-medical-clinic/\">View hospital details</a></p></div>", iconTerm: "hospital" }
];
And here's my function which is trying to access the data:
这是我试图访问数据的函数:
function setMarkers(map, sites) {
for (var i = 0; i < sites.length; i++) {
// var sites = markers[i];
var icon = '<?php bloginfo("template_url"); ?>/images/map-icons/' + sites[i].iconTerm + '.png';
var siteLatLng = new google.maps.LatLng(sites[i].latitude, sites[i].longitude);
var marker = new google.maps.Marker({
position: siteLatLng,
map: map,
icon: icon,
title: sites[i].title,
zIndex: sites[i].enumerator,
html: sites[i].html
});
var contentString = "Some content";
google.maps.event.addListener(marker, "click", function () {
infowindow.setContent(this.html);
infowindow.open(map, this);
});
}
}
I've also tried dropping back to just using the default Maps marker in case there was a specific issue with the iconTerm object but IE7/8 just fails trying to get the next object value.
我还尝试回退到只使用默认的 Maps 标记,以防 iconTerm 对象存在特定问题,但 IE7/8 只是尝试获取下一个对象值失败。
Here's a page on which this is occurring: http://devel.pixelapes.com/iaps.ie/surgeon/eamon-s-beausang/
这是发生这种情况的页面:http: //devel.pixelapes.com/iaps.ie/surgeon/eamon-s-beausang/
Strangely, I have another map instance where I'm not setting the bounds with map.fitBounds. The failure to get object values still occurs but the map sort of loads (but very messily): http://devel.pixelapes.com/iaps.ie/find-a-hospital-or-clinic/
奇怪的是,我有另一个地图实例,我没有用 map.fitBounds 设置边界。获取对象值的失败仍然发生,但地图加载(但非常混乱):http: //devel.pixelapes.com/iaps.ie/find-a-hospital-or-clinic/
I have tried removing the fitBounds functionality on the single surgeon page but the map still doesn't partially load as it does on the "Find a hospital" map.
我曾尝试在单个外科医生页面上删除 fitBounds 功能,但该地图仍然无法像在“查找医院”地图上那样部分加载。
Any ideas on how to get around this?
关于如何解决这个问题的任何想法?
回答by Dr.Molle
There are trailing comma's after the last items of sites
and latlngs
, remove them, IE doesn't like trailing commas in arrays/objects.
sites
and的最后一项之后有尾随逗号latlngs
,删除它们,IE 不喜欢数组/对象中的尾随逗号。
回答by Sean Mickey
I think your problem may be that IE does not like one (or more) of the property names you have chosen for the members of the sites
Array. I suggest changing your first code section to this:
我认为您的问题可能是 IE 不喜欢您为sites
数组成员选择的一个(或多个)属性名称。我建议将您的第一个代码部分更改为:
var sites = [{
"title": "Hermitage Medical Clinic",
"latitude": 53.3593139774,
"longitude": -6.40494071541,
"enumerator": 1,
"html": "<div class=\"find_a_surgeon_info_inner\"><h4 style=\"margin: 0; padding: 0;\">Hermitage Medical Clinic</h4><p>Old Lucan Road, Dublin 20</p><p><a href=\"http://devel.pixelapes.com/iaps.ie/location/hermitage-medical-clinic/\">View hospital details</a></p></div>",
"iconTerm": "hospital"
}];