从我的xml获取具有id的特定数据?

时间:2011-03-24 10:43:16

标签: jquery xml

我有一个脚本可以从XML获取我的数据并按照我的意愿将其附加到我的身体标签。

以下是代码:

$(document).ready(function()
{
    $.get('myData.xml', function(d){
    $('body').append('<h1> Recommended Web Development Books </h1>');
    $('body').append('<dl />');

    $(d).find('country').each(function(){

        var $myapp = $(this); 
        var title = $myapp.attr("title");
        var currency = $myapp.attr("currency");
        var capitol = $myapp.attr("capitol");
        var popul = $myapp.attr("popul");
        var lang = $myapp.attr("lang");
        var phone = $myapp.attr("phone");
        var taxi = $myapp.attr("taxi");
        var airports = $myapp.attr("airports");


        var description = $myapp.find('description').text();
        var imageurl = $myapp.attr('imageurl');

        var html = '<dt> <img class="bookImage" alt="" src="' + imageurl + '" /> </dt>';
        html += '<dd> <span class="loadingPic" alt="Loading" />';
        html += '<p class="b-title">' + title + '</p>';
        html += '<p class="title"> Para Birimi</p> <p>' + currency + '</p>';
        html += '<p class="title"> Başkent</p><p> ' + capitol + '</p>' ;
        html += '<p class="title"> Nüfus</p><p> ' + popul + '</p>' ;
        html += '<p class="title"> Dil</p><p> ' + lang + '</p>' ;
        html += '<p class="title"> Telefon Kodu</p><p> ' + phone + '</p>' ;
        html += '<p class="title"> Taksi</p><p> ' + taxi + '</p>' ;
        html += '<p class="title"> Havalanları</p><p> ' + airports + '</p>' ;
        html += '<p class="title"> Ne Zaman Gidilir</p><p> ' + description + '</p>' ;
        html += '</dd>';

        $('dl').append($(html));

        $('.loadingPic').fadeOut(1400);
    });
});

当我点击一个ID为从同一ID获取XML内容的链接时。

例如,有一个德国按钮,当我点击它时它的ID是“01”我想在空白页面或同一页面看到我的XML中的德国数据,无论如何。

2 个答案:

答案 0 :(得分:0)

在each()之前创建htmlArray变量并使其成为数组(或JSON对象)
比在每个()中将每种语言设置为不同的数组节点,如htmlArray ['de'] = html(如果语言是德语)或类似的JSON。
单击按钮上的单击事件,然后单击,然后测试语言并使用该语言的相应阵列节点。

答案 1 :(得分:0)

只需将ID添加到您的dl列表中,以便我们知道每个dtdd所在的国家/地区。然后根据点击按钮的dl显示id中的这两个元素。

$(document).ready(function()
{
    $.get('myData.xml', function(d){
    $('body').append('<h1> Recommended Web Development Books </h1>');
    $('body').append('<dl />');

    $(d).find('country').each(function(){

        var $myapp = $(this); 
        var title = $myapp.attr("title");
        var currency = $myapp.attr("currency");
        var capitol = $myapp.attr("capitol");
        var popul = $myapp.attr("popul");
        var lang = $myapp.attr("lang");
        var phone = $myapp.attr("phone");
        var taxi = $myapp.attr("taxi");
        var airports = $myapp.attr("airports");


        var description = $myapp.find('description').text();
        var imageurl = $myapp.attr('imageurl');
        var id = $myapp.attr('id');

        var html = '<dt class="' + id + '"> <img class="bookImage" alt="" src="' + imageurl + '" /> </dt>';
        html += '<dd class="' + id + '"> <span class="loadingPic" alt="Loading" />';
        html += '<p class="b-title">' + title + '</p>';
        html += '<p class="title"> Para Birimi</p> <p>' + currency + '</p>';
        html += '<p class="title"> Başkent</p><p> ' + capitol + '</p>' ;
        html += '<p class="title"> Nüfus</p><p> ' + popul + '</p>' ;
        html += '<p class="title"> Dil</p><p> ' + lang + '</p>' ;
        html += '<p class="title"> Telefon Kodu</p><p> ' + phone + '</p>' ;
        html += '<p class="title"> Taksi</p><p> ' + taxi + '</p>' ;
        html += '<p class="title"> Havalanları</p><p> ' + airports + '</p>' ;
        html += '<p class="title"> Ne Zaman Gidilir</p><p> ' + description + '</p>' ;
        html += '</dd>';

        $('dl').append($(html));

        $('.loadingPic').fadeOut(1400);
    });//end country.each

    $('input[id]').click(function() {
        $('dl > *').hide();
        $('dl .' + $(this).attr('id')).show();
    });//end input.click
    });//end $.get
});//end doc.ready

您还可以使用.hide().show().fadeOut().fadeIn()切换.slideUp().slideDown()