Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jquery解析html数据?_Javascript_Jquery - Fatal编程技术网

Javascript 如何使用jquery解析html数据?

Javascript 如何使用jquery解析html数据?,javascript,jquery,Javascript,Jquery,我在使用这段代码从html中提取数据时遇到了麻烦。谁能告诉我它有什么问题吗?下面是JSFIDLE中的示例 预期产出: SRC: ./season/123434mango.jpg HREF: /mango/ LOCATION:Europe PRICE:2 season:2 thumbnail:ok SUBJECT: read 1) 您错过了数据中的HREF。按 2) parent(您的变量)仅引用标记,因此.find()在此失败 var parent = $(this).parent(); pa

我在使用这段代码从html中提取数据时遇到了麻烦。谁能告诉我它有什么问题吗?下面是JSFIDLE中的示例

预期产出:

SRC: ./season/123434mango.jpg
HREF: /mango/
LOCATION:Europe
PRICE:2
season:2
thumbnail:ok
SUBJECT: read
1) 您错过了
数据中的
HREF
。按

2)
parent
(您的变量)仅引用标记,因此
.find()
在此失败

var parent = $(this).parent();
parent.parent().find(".location")  // fails eventually
因此,它应该是
parent.parent()


您遗漏的是图像的父对象是锚定标记。你想要的是:

var parent = $(this).parent().parent();
然后剩下的就都准备好了。显然,您也需要缺少的href片段,正如其他海报所提到的:

data.push({
        SRC: $(this).attr("src"),
        HREF: parent.attr("href"),  // you missed to add this 
        LOCATION: parent.find(".location").text(),
        price: parent.find(".price").text(),
        season: parent.find(".season").text(),
        thumbnail: parent.find(".thumbnail_label").text(),
        SUBJECT: parent.find(".subject li").text()
    });

您的一些jQuery遍历方法也不正确。parseHTML例程应该如下所示:

$($.parseHTML(htmlData)).each(function() {
    $(this).find("img").each(function() {
        var parent = $(this).parent();
        data.push({
            SRC: $(this).attr("src"),
            HREF: parent.attr("href"),
            LOCATION: parent.parent().find("div").next().closest('ul').next().find('.location').text(),
            PRICE: parent.parent().find("div").next().closest('ul').next().find('.price').text(),
            season: parent.parent().find("div").next().find('span').text(),
            thumbnail: parent.parent().find("div").html(),
            SUBJECT: parent.parent().find("div").next().closest('ul').find('li').text()
        }); //END .push
    }); //END .find("img")
}); //END .parseHTML

下面是一个更正的JSFIDLE:

img没有href参数a标记有
href
,例如,在
数据中从未定义a标记。push()
href行后缺少逗号谢谢大家的回复。现在href已被修复,但位置、价格、季节、缩略图标签和主题均为空!我怎样才能解决它们?@Pete不,我没有偷你的答案。我当时正忙着在JSFIDLE中工作,刚才我甚至没有加载你的答案,我看到了。谢谢user1671639和其他。现在它修好了。
data.push({
            SRC: $(this).attr("src"),
            HREF: parent.attr("href"), //missed to add it
            LOCATION: parent.parent().find(".location").text(),
            price: parent.parent().find(".price").text(),
            season: parent.parent().find(".season").text(),
            thumbnail: parent.parent().find(".thumbnail_label").text(),
            SUBJECT: parent.parent().find(".subject li").text()
        });
var parent = $(this).parent().parent();
data.push({
        SRC: $(this).attr("src"),
        HREF: parent.attr("href"),  // you missed to add this 
        LOCATION: parent.find(".location").text(),
        price: parent.find(".price").text(),
        season: parent.find(".season").text(),
        thumbnail: parent.find(".thumbnail_label").text(),
        SUBJECT: parent.find(".subject li").text()
    });
$($.parseHTML(htmlData)).each(function() {
    $(this).find("img").each(function() {
        var parent = $(this).parent();
        data.push({
            SRC: $(this).attr("src"),
            HREF: parent.attr("href"),
            LOCATION: parent.parent().find("div").next().closest('ul').next().find('.location').text(),
            PRICE: parent.parent().find("div").next().closest('ul').next().find('.price').text(),
            season: parent.parent().find("div").next().find('span').text(),
            thumbnail: parent.parent().find("div").html(),
            SUBJECT: parent.parent().find("div").next().closest('ul').find('li').text()
        }); //END .push
    }); //END .find("img")
}); //END .parseHTML