【问题标题】:Parsing XML with AJAX : Image Link : undefined?使用 AJAX 解析 XML:图像链接:未定义?
【发布时间】:2014-06-07 08:46:58
【问题描述】:

我正在解析 xml,我想要的是在解析后返回 xml。 我无法获取图片链接。你能帮助我吗 ?我的代码如下或http://jsfiddle.net/4DejY/2/:

HTML

<ul data-role="listview" data-filter="true" data-filter-placeholder="Search..." id="ds-canho">

javascript

$(document).ready(function () {
    $.mobile.loading("show");
    $.ajax({
        type: "GET",
        url: 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url=%27http://saigonpearl.info/trang-chu.html%27%20and%20xpath=%22//td[@class=%27table3%27]/table/tr[2]/td/table/tr/td/table/tr/td/a[contains(@href,%20%27http%27)]%20|%20//div[@class=%27tindang%27]/h3%20|%20//div[@class=%27detailsR%27]/p%22%20%20&diagnostics=true',
        dataType: "xml",
        success: parseXml
    });

    function parseXml(xml) {
        var gia, img, title;
        $(xml).find('p').each(function () {
            gia = $(this).text();
            title = $(this).prev('h3').text();

            $("ul#ds-canho").append('<li><img src="' + img + '">' + title + '<span class="ui-li-count"> ' + gia + '</span></li>')
            $('ul#ds-canho').listview('refresh');
            console.log(this);
        });

        $(xml).find("img").each(function () {
            img = $(this).find('src').text();
            console.log(this);
            $.mobile.loading("hide");
        });
    }
});

【问题讨论】:

  • 使用前定义img

标签: javascript jquery html ajax xml


【解决方案1】:

您需要在将 elem 附加到 DOM 之前定义您的 img

var gia, img, title;
$(xml).find('p').each(function() {
gia = $(this).text();
title = $(this).prev('h3').text();
img = $(this).prev('h3').prev('a').find('img').attr('src');
console.log(img);    
$("ul#ds-canho").append('<li><img src="'+ img +'">' + title + '<span class="ui-li-count"> '+gia+'</span></li>') 
$('ul#ds-canho').listview('refresh');
    $.mobile.loading( "hide" );

 });

这是一个小提琴:http://jsfiddle.net/4DejY/3/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多