Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery每个和onload问题_Jquery_Image - Fatal编程技术网

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);
            }
        });
    });
});