如何使用 jQuery 将图像注入到从 API 提取的图像列表中?
查看此网址时 http://dl.dropbox.com/u/1550420/jquery /flickr.html,我们看到从 API (flickr) 提取的图像列表。
我想注入以下图像 http://dl.dropbox。 com/u/1550420/jquery/chicago-008.jpg 到我的列表的第三个位置,从而增加了我的列表的长度。
总之,列表将在第 3 个位置插入 1 个图像。该图像将来自唯一的 url,而不是来自同一个 API。
我该如何做到这一点?
这是我的代码,以防上面的链接不起作用:
<script>
$(document).ready(function() {
$.getJSON("http://api.flickr.com/services/feeds/groups_pool.gne?id=675729@N22&lang=en-us&format=json&jsoncallback=?", function(data){
$.each(data.items, function(){
var raffie = '<a href="' + this.link + '"></a>';
$('<li></li>')
.append(raffie)
.appendTo('#pic');
$('<img />').attr('src', this.media.m)
.appendTo('#pic');
});
});
});
</script>
这是正文中的硬编码 html:
<ul><li id="pic"></li></ul>
When viewing this url http://dl.dropbox.com/u/1550420/jquery/flickr.html, we see a list of images pulling from an API (flickr).
I’d like to inject the following image http://dl.dropbox.com/u/1550420/jquery/chicago-008.jpg into the 3rd position of my list, thus increasing the length of my list.
In summary, the list would have 1 image inserted into it at the 3rd position. The image would come from a unique url, not from the same API.
How do I accomplish that?
Here's my code, in case the link above doesn't work:
<script>
$(document).ready(function() {
$.getJSON("http://api.flickr.com/services/feeds/groups_pool.gne?id=675729@N22&lang=en-us&format=json&jsoncallback=?", function(data){
$.each(data.items, function(){
var raffie = '<a href="' + this.link + '"></a>';
$('<li></li>')
.append(raffie)
.appendTo('#pic');
$('<img />').attr('src', this.media.m)
.appendTo('#pic');
});
});
});
</script>
and here's the hard-coded html from the body:
<ul><li id="pic"></li></ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将所有图像附加到 DOM 后,您应该选择第二个图像,并在其后面插入其他图像:
这是小提琴: http://jsfiddle.net/LbtWJ/。
PS 你不应该将图像单独附加到 DOM,但这不是重点。
After you're done appending all your images to the DOM, you should select the 2nd image, and insert that other image after it:
Here's the fiddle: http://jsfiddle.net/LbtWJ/ .
P.S. You shouldn't be appending your images to the DOM separately, but that's besides the point.
在您的代码中,它看起来像是将
li
附加到另一个li
中,这是不允许的。这个怎么样。标记如下所示:(
并不是真正必要的,因为我们要覆盖它,但这样页面就可以验证)
然后 JS 代码如下所示:
In your code, it looks like appending
li
into anotherli
, which is not allowed.How about this. Markup looks like this: (
<li>
is not really necessary since we are overwriting it, but this way page validates)Then the JS code looks like this: