jQuery数组对象获取值

时间:2014-11-04 20:03:53

标签: javascript jquery arrays xml parsing

嘿,我有以下代码:

var items = [];
$(xml).find("Placemark").each(function () {
    var tmp_latLng = $(this).find("coordinates").text();

    tmp_latLng = tmp_latLng.split(",");

    items.push({
        name: $(this).find("name").text(),
        snippet: $(this).find("Snippet").text(),
        description: $(this).find("description").text(),
        lat: tmp_latLng[0],
        lng: tmp_latLng[1],
        distance: $(this).find("distance").text()
    });
});

var markerNodes = items;

if (markerNodes.length > 0) {
     howManyMarkers = markerNodes.length;

     for (var i = 0; i < markerNodes.length; i++) {
         console.log(markerNodes[i].Snippet);

         var name = markerNodes[i].getAttribute("Snippet");
         var address = markerNodes[i].getAttribute("Snippet");
         var description = markerNodes[i].getAttribute("description");
         var distance = parseFloat(markerNodes[i].getAttribute("distance"));
         var latlng = new google.maps.LatLng(parseFloat(markerNodes[i].getAttribute("lat")), parseFloat(markerNodes[i].getAttribute("lng")));

         createOption(name, distance, i);
         createMarker(latlng, name, address);
         bounds.extend(latlng);
     }

     map.fitBounds(bounds);
} else {
     alert('Sorry, there are no stores that close to your location. Try expanding your search radius.');
}

错误发生在 console.log(markerNodes [i] .Snippet); 。错误是 Uncaught TypeError:undefined不是函数

markerNodes [i] .getAttribute(“Snippet”)的错误为未捕获的TypeError:undefined不是函数

我会失踪什么?

3 个答案:

答案 0 :(得分:0)

应该是

markerNodes[i].snippet

(注意案例)

答案 1 :(得分:0)

JavaScript区分大小写。属性代码段为小写,请使用

console.log(markerNodes[i].snippet);

答案 2 :(得分:0)

Javascript中的标识符区分大小写,因此它应为snippet,而不是Snippet

console.log(markerNodes[i].snippet);

getAttribute method与元素一起使用,而不是对象。只需作为对象的成员访问该属性:

var name = markerNodes[i].snippet;
var address = markerNodes[i].snippet;
var description = markerNodes[i].description;
var distance = parseFloat(markerNodes[i].distance);
var latlng = new google.maps.LatLng(parseFloat(markerNodes[i].lat), parseFloat(markerNodes[i].lng));

注意:也许您希望从name属性而不是name属性中获取snippet变量的值。