2013-01-08 66 views
0

这里是我的js代码:jQuery的:无法读取XML元素值

$(document).ready(function(){ 
     $.ajax({ 
     type: "GET", 
     url: "vector.xml", 
     dataType: "xml", 
     success: function(xml) { 
          $(xml).find('Vector').each(function() { 
          //var attrVal = $(this).find('VectorType').attr('tc'); 
          var intialVal = $('this').find('InitialValue').text(); 
          var attrVal = $(this).find('VectorMode').attr('tc'); 
          alert(intialVal); 

         }); 
       } 
      }); 
    }); 

XML看起来是这样的:

<Values> 
     <Result> 
      <ResultBasis> 
      <Vector> 
       <VectorType tc="71"></VectorType> 
       <VectorBaseDate>2012-06-16</VectorBaseDate> 
       <InitialValue>150000</InitialValue> 
      </Vector> 
      <Vector tc="T20V09"> 
       <VectorType tc="71"></VectorType> 
       <VectorBaseDate>2012-06-16</VectorBaseDate> 
       <InitialValue>150000</InitialValue> 
      </Vector> 
      <Vector> 
       <VectorType tc="5"></VectorType> 
       <VectorBaseDate>2012-06-16</VectorBaseDate> 
       <VectorMode tc="4"></VectorMode> 
       <InitialValue>102.47</InitialValue> 
      </Vector> 
      <Vector> 
       <VectorType tc="5"></VectorType> 
       <VectorBaseDate>2012-06-16</VectorBaseDate> 
       <VectorMode tc="3"></VectorMode> 
       <InitialValue>307.42</InitialValue> 
      </Vector> 

在这里,我能够属性的VectorTypeVectorMode元素的值但InitialValueVectorBaseDate元素值没有运气。

alert(var intialVal = $('this').find('InitialValue').val()); 

给我undefined

alert(var intialVal = $('this').find('InitialValue').text()); 

给我空的警告语句。

Note: <script src="jquery-1.8.1.min.js"></script> 

回答

0

我不知道的jQuery的版本,但我知道在Javascript中它是你正在寻找的nodeValue。

0

尝试使用this代替'this'

var intialVal = $(this).find('InitialValue').text();