我无法使用这个损坏的代码从 html 中提取数据。谁能告诉我它有什么问题?这是 jsfiddle 中的示例。http://jsfiddle.net/AjM7u/3/
var data = [];
var htmlData = '<li>';
htmlData += ' <a href="/mango/" >';
htmlData += ' <img src="./season/123434mango.jpg" width="180" height="148"';
htmlData += ' alt="mango season" class="png"></a>';
htmlData += '';
htmlData += ' ';
htmlData += ' <div class="thumbnail_label">ok</div>';
htmlData += ' ';
htmlData += '';
htmlData += ' <div class="details">';
htmlData += ' <div class="title">';
htmlData += ' <a href=';
htmlData += ' "/mango/"> mango</a>';
htmlData += ' <span class="season">2</span>';
htmlData += ' </div>';
htmlData += ' <ul class="subject">';
htmlData += ' <li>read</li>';
htmlData += ' </ul>';
htmlData += ' <ul class="sub-info">';
htmlData += ' <li class="location">Europe</li>';
htmlData += ' <li class="price">2</li>';
htmlData += ' </ul>';
htmlData += ' </div>';
htmlData += '</li>';
console.log($.parseHTML(htmlData));
$($.parseHTML(htmlData)).each(function() {
$(this).find("img").each(function() {
var parent = $(this).parent();
data.push({
SRC: $(this).attr("src"),
HREF: parent.find("a").attr("href"),
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()
});
});
});
function outputData() {
var html = "";
for(var i = 0; i < data.length; i++) {
html += "SRC: " + data[i].SRC;
html += "<br/>HREF: " + data[i].HREF;
html += "<br/>LOCATION: " + data[i].LOCATION;
html += "<br/>PRICE: " + data[i].price;
html += "<br/>season: " + data[i].season;
html += "<br/>thumbnail: " + data[i].thumbnail;
html += "<br/>SUBJECT: " + data[i].SUBJECT;
html += "<br/><br/>";
}
return html;
}
$("#output").html(outputData());
输出:
SRC: ./season/123434mango.jpg
HREF: undefined
LOCATION:
PRICE:
season:
thumbnail:
SUBJECT:
预期输出:
SRC: ./season/123434mango.jpg
HREF: /mango/
LOCATION:Europe
PRICE:2
season:2
thumbnail:ok
SUBJECT: read