1

我从我的数据库中加载了多个图像onload="imgLoaded($(this));",当被调用时,可以使图像具有可拖动、可调整大小和可删除的能力。

图片:

<img src='data:image/".$ext.";base64,".base64_encode(XXXXX)."' style=\"width:inherit; height:inherit;\" class='img_set' onload=\"imgLoaded($(this));\">

函数imgLoaded:

function imgLoaded(imgElemt)
{
    $('.db_result').html('<img src="images/loader.gif" />'); 
    var full_url = document.URL;

    if(full_url.indexOf('idedit') <= 0 || imgAreaMap > 0){
        if(imgElemt.closest(".child").width() < imgElemt.width() || imgElemt.closest(".child").height() < imgElemt.height()){
            if(imgElemt.closest(".child").width() > imgElemt.closest(".child").height()){ 
                imgElemt.parent(".imgh").height("100%"); 
                imgElemt.parent(".imgh").width(imgElemt.width()); 
            }else{ 
                imgElemt.parent(".imgh").width("100%"); 
                imgElemt.parent(".imgh").height(imgElemt.height());
            }
        }
        else{
            imgElemt.parent(".imgh").width(imgElemt.width());
        }
    }


    $('.db_result').delay(500).queue(function(n) { $(this).html(''); });
    $('#liveDimensions').text('Largura: '+imgElemt.width()+' px\nAltura: '+imgElemt.height()+' px');
    $('.imgh').on( 'resize', function( event, ui ) {
        $('#liveDimensions').text('Largura: '+$(this).width()+' px\nAltura: '+$(this).height()+' px');
    });
    imgElemt.parent('.imgh').append('<div class=\"close\"><img src=\"images/delete.png\"/></div>');
    imgElemt.closest('.child').children('.fileinput-holder').remove();
    imgElemt.closest('.imgh').draggable({ containment: imgElemt.closest('.child'), scroll: true, snap: true, snapTolerance: 5 });
    imgElemt.closest('.imgh').resizable({ containment: imgElemt.closest('.child') });
}

我的问题是只有最后加载的图像调用函数 imgLoaded。

如何强制每个加载的图像发生所有 onload?

4

1 回答 1

2

使用会更好

$( window ).load(function() {
  // Run code when all graphics have loaded
  // And here you can treat all your images at once
  // Find images using jQuery
  $("img").each(function() {
    // and apply your existing code
    imgLoaded($(this)) ; 
  }) ;
});

而不是将 1000 个加载事件附加到 1000 个不同的图像(在您的情况下这似乎不起作用)。

于 2013-09-19T23:12:03.230 回答