javascript - 使用 onload 加载多个图像以调用函数,只有最后一个加载调用函数

标签 javascript jquery

我使用 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。

我如何强制对每个加载的图像进行所有加载?

最佳答案

最好用

$( 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 个不同的图像(这在您的情况下似乎无论如何都不起作用)。

关于javascript - 使用 onload 加载多个图像以调用函数,只有最后一个加载调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18906370/

相关文章:

javascript - 将对象键值作为数组进行迭代

带有selenium 2的javascript下拉菜单

javascript - 我可以使用 AJAX + 跨域 + jsonp 测试 URL 是否可达吗?

JQuery - 弹出显示父跨度

jquery - 如何在 onblur 之前先触发 onclick?

javascript - $.ajax 方法不调用 Controller 操作方法

javascript - jQuery - 使用 .prepend()

javascript - 哪种基于Web的(在浏览器中)编辑器最适合“高级用户”?

javascript - 调整内容大小时调整饼图大小

javascript - 如何在选择网格内的下拉菜单时停止刷新剑道网格?