javascript - 使用AJAX解析XML : Image Link : undefined?

标签 javascript jquery html ajax xml

我正在解析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");
        });
    }
});

最佳答案

在将 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" );

 });

这是一个 fiddle :http://jsfiddle.net/4DejY/3/

关于javascript - 使用AJAX解析XML : Image Link : undefined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24092385/

相关文章:

javascript - element.event 和 element.setAttribute(event,handler) 之间的区别

jquery - 如何在 jquery 和 laravel 中通过 request 传递数据表中的两个参数

html - css动画不向右滚动

javascript - 如何检查字母是否为粗体?

javascript - 如何使用 browserify 捆绑主干应用程序?

javascript - 无法导出 vue-router : warning in ./src/router/index.js "export ' 默认'(导入为 'VueRouter' )在 'vue-router' 中找不到

javascript - 如何通过单击一个来选择两个表格单元格?

javascript - 如何将数据传递到 JQuery 脚本

javascript - 如何在 div 上使用 onclick 开始转换?

javascript - Cocos2D JS Vec2 未定义