【问题标题】:jQuery: Not able to read to xml element valuejQuery:无法读取 xml 元素值
【发布时间】:2013-01-08 19:42:31
【问题描述】:

这是我的js代码:

$(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>

【问题讨论】:

    标签: jquery xml xml-parsing


    【解决方案1】:

    我不确定 jQuery 版本,但我知道在 Javascript 中您正在寻找的是 nodeValue。

    【讨论】:

      【解决方案2】:

      尝试使用this 而不是'this'

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-23
        • 2017-10-05
        • 2019-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多