jquery每个和onload问题
我有以下代码jquery每个和onload问题,jquery,image,Jquery,Image,我有以下代码 $(function () { $.post("fetch_data.php?url="+$('#url').val(), { }, function(response){ var arrValues = [ "path/to/img1", "path/to/img2", "path/to/img3", "path/to/img4" ]; //output from php var img = new Image(); $.e
$(function () {
$.post("fetch_data.php?url="+$('#url').val(), { }, function(response){
var arrValues = [ "path/to/img1", "path/to/img2", "path/to/img3", "path/to/img4" ]; //output from php
var img = new Image();
$.each(arrValues, function( intIndex, objValue ) {
img.src = objValue;
img.onload = function() {
alert('height: ' + img.height + ' width: ' + img.width);
}
});
});
});
我是javascript/jquery新手,这段代码只返回图像的lastpath/to/img4高度/宽度
如何使其返回数组的所有图像和宽度?由于参数img是在循环外部定义的,它将仅指向上次创建的img 尝试在$内声明img。每个循环
并在警报中用此替换img。我想您必须创建四个图像元素。。。你必须稍微修改一下你的代码 试试下面的代码
$(function () {
$.post("fetch_data.php?url="+$('#url').val(), { }, function(response){
var arrValues = [ "path/to/img1", "path/to/img2", "path/to/img3", "path/to/img4" ]; //output from php
$.each(arrValues, function( intIndex, objValue ) {
var img = new Image(); //creating number of Image Elements equal to arrValues length
img.src = objValue;
img.onload = function() {
alert('height: ' + img.height + ' width: ' + img.width);
}
});
});
});