2011-12-29 56 views
1

我正在使用JQuery的.get函数来检索XML文件。当试图读取文件的值时,所有变量都是空的。JQuery XML读取 - 空变量

这里就是我在检索和读取XML文件中的函数:

var get_locations = function(neLat, neLng, swLat, swLng, map, infoWindow) 
     { 
      $.get('Scripts/googlemap_ajax.php', {neLat: neLat, neLng: neLng, swLat: swLat, swLng: swLng}, 
      function(xml) 
      { 
       $(xml).find("marker").each(function() 
       { 
        var name = $(this).find('name').text(); 
        var tags = $(this).find('tags').text(); 
        var coupon = $(this).find('coupon').text(); 
        var icon = customIcons[coupon] || {}; 
        var point = new google.maps.LatLng(
           $(this).find('latitude').text(), 
           $(this).find('longitude').text()); 
     alert($(this).find('name').text()); 
        var marker = new google.maps.Marker({ 
         position: point, 
         map: map, 
         icon: icon.icon 
        }); 


        var html = "HELLO THERE"; 

        google.maps.event.addListener(marker, 'click', function() { 
          infoWindow.setContent(html); 
          infoWindow.open(map, marker); 
        }); 
       }); 
      }, "xml"); 
     }; 

当代码运行时,我得到一个警报,提醒的$(this).find('name').text()值为空。

这里是(通过Firebug的验证)相应的XML文件:

<markers> 
<marker locationID="1" name="Chris' Food Place" tags="American, Sushi" latitude="34.113432" longitude="-117.169855" coupon="yes"></marker> 
<marker locationID="2" name="Chris' Food Place 2" tags="American," latitude="34.1158092" longitude="-117.1704145" coupon="no"></marker> 
</markers> 

这里是整个代码块,以防万一有人碰巧发现了其他的问题引起:

<style type="text/css" > 

    html { height: 100% } 

    body { height: 100%; margin: 0; padding: 0 } 

</style> 



<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script> 
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script> 


<script type="text/javascript"> 

$(document).ready(function() 
{ 
    var alerted = false; 

    function MYMAP() 
    { 
     //internal 
     var map = ''; 
     var center = ''; 
     var centerImage = '../Images/map_icon.png'; 
     var infoWindow = ''; 
     var last_ne_lat; //Check previous map corner 

     var customIcons = { 
       yes: { 
       icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png', 
       shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' 
       }, 
       no: { 
       icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png', 
       shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' 
       } 
     }; 

     var get_locations = function(neLat, neLng, swLat, swLng, map, infoWindow) 
     { 
      $.get('Scripts/googlemap_ajax.php', {neLat: neLat, neLng: neLng, swLat: swLat, swLng: swLng}, 
      function(xml) 
      { 
       $(xml).find("marker").each(function() 
       { 
        var name = $(this).find('name').text(); 
        var tags = $(this).find('tags').text(); 
        var coupon = $(this).find('coupon').text(); 
        var icon = customIcons[coupon] || {}; 
        var point = new google.maps.LatLng(
           $(this).find('latitude').text(), 
           $(this).find('longitude').text()); 
     alert($(this).find('name').text()); 
        var marker = new google.maps.Marker({ 
         position: point, 
         map: map, 
         icon: icon.icon 
        }); 


        var html = "HELLO THERE"; 

        google.maps.event.addListener(marker, 'click', function() { 
          infoWindow.setContent(html); 
          infoWindow.open(map, marker); 
        }); 
       }); 
      }, "xml"); 
     }; 


    //external: 
    return { 
      init: function(selector, latLng, zoom) 
      { 
       var myOptions = { 
         zoom:zoom, 
         center: latLng, 
        streetViewControl: false, 
         mapTypeId: google.maps.MapTypeId.ROADMAP 
       } 
       this.map = new google.maps.Map($(selector)[0], myOptions); 
      }, 

      centerMarker: function(latlng) 
      { 
       this.center = new google.maps.Marker({ 

       position: latlng, 

       map: this.map, 

       icon: centerImage, 

       title: "You are here" 

       }); 
      }, 

      addBoundChange: function() 
      { 
       this.infoWindow = new google.maps.InfoWindow; 
       test = this.map; 
       test2 = this.infoWindow; 
       // Add listener to map 
       google.maps.event.addListener(this.map, 'bounds_changed', function() { 
        var zoom_level = this.getZoom(); 
        if(zoom_level > 12) 
        { 
         var bounds = this.getBounds(); 
         var ne = bounds.getNorthEast(); 
         var neLat = bounds.getNorthEast().lat(); 
         var neLng = bounds.getNorthEast().lng(); 
           var sw = bounds.getSouthWest(); 
         var swLat = bounds.getSouthWest().lat(); 
         var swLng = bounds.getSouthWest().lng(); 
         if(neLat != last_ne_lat) 
         { 
          last_ne_lat = neLat;  
          get_locations(neLat, neLng, swLat, swLng, test, test2); 

         } 
        } 
        else //Alerts the user only once. 
        { 
         if(!alerted)  
         { 
          alerted=true; 
          alert("Please zoom in to continue displaying the location markers"); 
         } 
        } 
       }); 
      } 

     }; 
    } 
    var testMap = new MYMAP(); 
    var latlng = new google.maps.LatLng(<?php echo $lat; ?>,<?php echo $long; ?>); 
    testMap.init('#map_canvas', latlng, 16); 
    testMap.centerMarker(latlng); 
    testMap.addBoundChange(); 

}); 
</script> 


<body> 
    <div id="locationSelect" style="width:100%"></div> 

    <div id="map_canvas" style="width:70%; height:90%"></div> 

</body> 

回答

0

您的目标是属性,而不是标签。尝试$(本).attr( “名称”)代替$(本).find( '名')。文本()

0

.find(选择)用于遍历每个后代在当前匹配的元素集合

.attr(的attributeName)的元件被用于获取在匹配的元素

您正在尝试访问XML的属性的用于第一元素的属性的值元件。 使用$(this).attr(attributeName)来获取值。