我的测试页面当前正在显示:Test Page这是一个很好的标志,但是显示不正确。 我有这些jQuery对象:
reviews: Array[3]
0: Object
excerpt: "Everything I have had here is insane. SO GOOD. I always get a tuna baguette. Normally, the bread on sandwiches is just what holds it together, but here it..."
id: "yVT2R7JQ5WRgVXZ-LrnJhQ"
rating: 5
rating_image_large_url: "http://media1.ak.yelpcdn.com/static/20101216354709277/img/ico/stars/stars_large_5.png"
rating_image_small_url: "http://media3.ak.yelpcdn.com/static/201012161949604803/img/ico/stars/stars_small_5.png"
rating_image_url: "http://media3.ak.yelpcdn.com/static/201012162578611207/img/ico/stars/stars_5.png"
time_created: 1323827891
user: Object
__proto__: Object
1: Object
2: Object
我希望它只显示一个“来自Yelp.com的44条评论”而不是10 ...我无法弄清楚如何正确地进入这个对象树并定位正确的元素。我使用这个脚本用这个脚本显示它们但我无法弄清楚为什么它没有正确显示?
function showData(data) {
$.each(data.name, function(i,business){
// extra loop
var bizContent = '<p><img src="' + data.rating_img_url + '" img=""/><br><a href="'+ data.url +'">'+ data.review_count + ' reviews from Yelp.com</a></p>';
$(bizContent).appendTo('#yelpAVG');
$.each(data.reviews, function(i,review){
var content = '<div class="comments-block"><p>Posted by <a href="'+review.user_url+'">' +review.user_name + ' </a> on ' + review.date + 'via <a href="'+review.url+'">Yelp.com</a>';
content += '<img src="' + review.user_photo_url + '" img=""/>';
content += '<p><img src="' + review.rating_img_url + '" img=""/><br>';
content += review.text_excerpt + '</p>';
content += '<p><a href="'+review.url + '">Read the full review</a><br>';
$(content).appendTo('#yelpReviews');
});
});
};
任何帮助或方向都是满意的!!!! 感谢阅读
答案 0 :(得分:0)
尝试将bizContent
追加移出循环:
function showData(data) {
var bizContent = '<p><img src="' + data.rating_img_url + '" img=""/><br><a href="' + data.url + '">' + data.review_count + ' reviews from Yelp.com</a></p>';
$(bizContent).appendTo('#yelpAVG');
$.each(data.name, function(i, business) {
// extra loop
$.each(data.reviews, function(i, review) {
var content = '<div class="comments-block"><p>Posted by <a href="' + review.user_url + '">' + review.user_name + ' </a> on ' + review.date + 'via <a href="' + review.url + '">Yelp.com</a>';
content += '<img src="' + review.user_photo_url + '" img=""/>';
content += '<p><img src="' + review.rating_img_url + '" img=""/><br>';
content += review.text_excerpt + '</p>';
content += '<p><a href="' + review.url + '">Read the full review</a><br>';
$(content).appendTo('#yelpReviews');
});
});
};
我不确定你是否真的需要第一个循环。