我试图让json数据闪烁图像,但我有点生疏,无法正常启动它。
很高兴得到一个小帮助,这样我就可以得到一个更好的主意。
以下代码段:
(function() {
//changed the callback so that it is defined
window.cb = function(data) {
$.each(data.items, function(i, item) {
$("<img>").attr("src", item.media.m).appendTo("#images");
if (i === 3) {
return false;
}
});
console.log(data);
}
var tags = "london";
var script = document.createElement('script');
script.src = "http://api.flickr.com/services/feeds/photos_public.gne?format=json&jsoncallback=cb&tags=" + tags;
document.head.appendChild(script);
})();
&#13;
#images {
height: 185px;
width: 240px;
padding: 0;
margin: 0;
overflow: hidden;
}
#images img {
border: none
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="images"></div>
&#13;
答案 0 :(得分:0)
迭代data.items并从media.m:
创建img标签
(function() {
//changed the callback so that it is defined
window.cb = function(data) {
console.log(data);
var $images = $('#images');
for(var i = 0; i < data.items.length; i++) {
$('<img/>').attr('src', data.items[i].media.m).appendTo($images);
}
};
var tags = "london";
var script = document.createElement('script');
script.src = "http://api.flickr.com/services/feeds/photos_public.gne?format=json&jsoncallback=cb&tags=" + tags;
document.head.appendChild(script);
})();
#images {
height: 185px;
width: 240px;
padding:0;
margin:0; overflow: auto;}
#images img {
border:none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="images"></div>