如何使用ajax从所有帖子中获取第一个图像
How get first image from all posts with ajax
我有这样的代码,它只为第一篇文章吐出所有内容。我希望它能吐出所有帖子中的第一张图片。
$.ajax({url: "http://api.tumblr.com/v2/blog/sjtest1.tumblr.com/posts?api_key=mykey",
dataType: 'jsonp',
success: function(results){
console.log(results);
var postBody = results.response.posts[0].body;
$(".x").html(postBody);
}});
这是一个ajax响应:
{
"meta": {
"status": 200,
"msg": "OK"
},
"response": {
"blog": { ... },
"posts": [
{
"blog_name": "citriccomics",
"id": 3507845453,
"post_url": "http:'/'/citriccomics.tumblr.com'/post'/3507845453",
"type": "text",
"date": "2011-02-25 20:27:00 GMT",
"timestamp": 1298665620,
"state": "published",
"format": "html",
"reblog_key": "b0baQtsl",
"tags": [
"tumblrize",
"milky dog",
"mini comic"
],
"note_count": 14,
"title": "Milky Dog",
"body": "<p><img src='"http:'/'/media.tumblr.com'
/tumblr_lh6x8d7LBB1qa6gy3.jpg'"'/><a href='"http:'/'
/citriccomics.com'/blog'/?p=487'" target='"_blank'">TO READ
THE REST CLICK HERE<'/a><br'/>'n'nMilky Dog was inspired by
something <a href='"http:'/'/gunadie.com'/naomi'"
target='"_blank'">Naomi Gee<'/a> wrote on twitter, I really
liked the hash tag <a href='"http:'/'/twitter.com'/
search?q=%23MILKYDOG'" target='"_blank'">#milkydog<'/a>
and quickly came up with a little comic about it. You can
(and should) follow Naomi on twitter <a href='"http:'/'
/twitter.com'/ngun'" target='"_blank'">@ngun<'/a> I'm on
twitter as well <a href='"http:'/'/twitter.com'
/weflewairplanes'"target='"_blank'">@weflewairplanes<'/a>
<'/p>'n'nAlso, if you’re a Reddit user (or even if
you're not) I submitted this there, if you could up vote
it I'd be super grateful just <a href='"http:'/'
/tinyurl.com'/5wj3tqz'" target='"_blank'">CLICK HERE<'/a>"
},
...
],
"total_posts": 3
}
}
正如您所看到的,身体中的图像被
标签包裹。我是如何获得图像的?
使用jQuery,查看示例代码:
$(results.response.posts[0].body).find('img').first();
这可能不是最好的选择,但它应该这样做。如果你想为多个帖子做这件事,请使用图像阵列:
var img = new Array();
$.each(results.response.posts, function (ix,el){
img.push($(el.body).find('img').first());
})
试试这个:
$.ajax({url: "http://api.tumblr.com/v2/blog/sjtest1.tumblr.com/posts?api_key=mykey",
dataType: 'jsonp',
success: function(results){
console.log(results);
var postBody = results.response.posts[0].body;
var html = $(postBody);
var firstImage = $(postBody).find("img:first");
}});
var $ct = $('.x');
$.ajax({
url: "http://api.tumblr.com/v2/blog/sjtest1.tumblr.com/posts?api_key=mykey",
dataType: 'jsonp',
success: function (results) {
console.log(results);
$.each(results.response.posts, function (_, post) {
$(post.body).find('img:first').appendTo($ct)
})
}
});
相关文章:
- RxJS油门行为;立即获取第一个值
- 与杜兰达尔合作的第一个JavaScript项目.尝试从第三方 API 获取数据
- 如何通过连接多个异步javascript调用,从对象数组中排序并获取第一个obejct
- 只获取HTML元素的第一个类
- 获取行中的第一个值“;点击“;并传递给控制器
- 获取此行的第一个 TD
- 使用 javascript 获取文本框中输入的第一个值
- 使用 querySelectorAll 获取具有该类名的所有元素,而不仅仅是第一个
- Vue js 获取数据模型的第一个值
- 从对象中的 javascript 数组中获取第一个值
- 如何使用 JQuery 获取表的第一个 td 的值
- 获取列表中的第一个 DOM 元素
- jQuery 如何获取第一个 LI 父级
- Javascript从数组中获取第一个值,然后跳过它
- 如何使用JavaScript从JSON字典中获取第一个值
- jQuery只从循环中获取第一个id值
- Javascript只从foreach循环中获取第一个id.为什么
- 如何在jQuery中默认获取第一个HTML选项的值
- 获取第一个h1标题,并在新选项卡中打开
h1.html - 从json中获取第一个值