用户jQuery从XML获取嵌套元素 [英] User jQuery to get nested elements from XML

查看:60
本文介绍了用户jQuery从XML获取嵌套元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为此而努力.如何从下面的XML的以下嵌套元素中获取值(我也将代码放在下面)?我在"descShort"值之后,然后是大写"Last"和大写"change":

I'm spinning my wheels on this. How do I get the values from the following nested elements from the XML below (I've also put my code below)? I am after the "descShort" value and then the capital "Last" and capital "change" :

<indices>
<index>
    <code>DJI</code>
    <exchange>NYSE</exchange>
    <liveness>DELAYED</liveness>
    <indexDesc>
        <desc>Dow Jones Industrials</desc>
        <descAbbrev>DOW JONES</descAbbrev>
        <descShort>DOW JONES</descShort>
        <firstActive></firstActive>
        <lastActive></lastActive>
    </indexDesc>
    <indexQuote>
        <capital>
            <first>11144.57</first>
            <high>11153.79</high>
            <low>10973.92</low>
            <last>11018.66</last>
            <change>-125.9</change>
            <pctChange>-1.1%</pctChange>
        </capital>
        <gross>
            <first>11144.57</first>
            <high>11153.79</high>
            <low>10973.92</low>
            <last>11018.66</last>
            <change>-125.9</change>
            <pctChange>-1.1%</pctChange>
        </gross>
        <totalEvents>4</totalEvents>
        <lastChanged>16-Apr-2010 16:03:00</lastChanged>
    </indexQuote>
</index>
<index>
    <code>XAO</code>
    <exchange>ASX</exchange>
    <liveness>DELAYED</liveness>
    <indexDesc>
        <desc>ASX All Ordinaries</desc>
        <descAbbrev>All Ordinaries</descAbbrev>
        <descShort>ALL ORDS</descShort>
        <firstActive>06-Mar-1970</firstActive>
        <lastActive></lastActive>
    </indexDesc>
    <indexQuote>
        <capital>
            <first>5007.30</first>
            <high>5007.30</high>
            <low>4934.00</low>
            <last>4939.40</last>
            <change>-67.9</change>
            <pctChange>-1.4%</pctChange>
        </capital>
        <gross>
            <first>5007.30</first>
            <high>5007.30</high>
            <low>4934.00</low>
            <last>4939.40</last>
            <change>-67.9</change>
            <pctChange>-1.4%</pctChange>
        </gross>
        <totalEvents>997</totalEvents>
        <lastChanged>19-Apr-2010 17:02:54</lastChanged>
    </indexQuote>
</index>

$.ajax({
            type: "GET",
            url: "stockindices.xml",
            dataType: "xml",
            success: function(xml) {
                $(xml).find('index').each(function(){

                    var self = $(this);                     
                    var code = self.find('indexDesc');

                    $(code).find('indexDesc').each(function(){
                        alert(self.find('descShort').text());
                    });                     

                    $('<span class=\"tickerItem\"></span>').html(values[0].text()).appendTo('#marq');                                                                   
                });
            }
        });

推荐答案

您的函数存在一些缺陷.这应该做到:

You have some flaws in your function. This should do it:

success: function(xml) {
    $(xml).find('index').each(function(){

        var value = $(this).find('indexDesc descShort').text();
        value += ' ' + $(this).find('indexQuote capital last').text();
        value += ' ' + $(this).find('indexQuote capital change').text();

        $('<span class="tickerItem"></span>').text(value).appendTo('#marq');
    });
}


您的代码有两个注释:


Two comments on your code:

var code = self.find('indexDesc');

$(code).find('indexDesc').each(function(){
       alert(self.find('descShort').text());
}); 

在这里,将元素indexDesc分配给变量code,随后尝试在indexDesc(不存在)中找到元素indexDesc.

Here you assign the element indexDesc to the variable code and later you try to find the element indexDesc inside indexDesc (which does not exist).

$('<span class=\"tickerItem\"></span>').html(values[0].text())

我真的很想知道values的来源,但从未声明.而且您无需在单引号中转义双引号.

I really wonder where values comes from, it is never declared. And you don't need to escape double quotes in single quotes.

我真的建议阅读文档

I really recommand to read the documentation and a tutorial to get the basics of jQuery.

这篇关于用户jQuery从XML获取嵌套元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆