如何從jquery json解析中獲取圖像大小?我把一個each function
,首先下載所有的圖像到html部分,然後嘗試使用width()
& height()
的圖像大小,但我遇到了一些麻煩在獲取width()
& height()
超前的圖像加載。在http://jsfiddle.net/7eLj4/如何從jquery json解析中獲取圖像大小?
jQuery代碼
示例代碼:
$(document).ready(function(){
var data = '[{"image":"http:\/\/pcdn.500px.net\/6269271\/a85b2995b4d80748831492be5e8a3689895a2b0e\/4.jpg"},{"image":"http:\/\/pcdn.500px.net\/5953805\/d0dd841969187f47e8ad9157713949b4b95b3bda\/4.jpg"},{"image":"http:\/\/pcdn.500px.net\/5735750\/0dd26cb9f53b7bbbedf1b65c0d0aac013f77466c\/4.jpg"}]';
var obj = $.parseJSON(data);
$.each(obj, function(index,item) {
$('#imagewrap').append('<img class="images" src="'+item.image+'" />');
});
$('.images').each(function(){
var width = $(this).width();
var height = $(this).height();
var imgsrc = $(this).attr('src');
alert(width+''+height+''+imgsrc);
});
});
HTML代碼:
<div id="imagewrap"></div>