Javascript 使用onload to call函数加载多个图像,并且只有最后一个加载的图像调用该函数
我从数据库中加载了多个图像,其中包含一个Javascript 使用onload to call函数加载多个图像,并且只有最后一个加载的图像调用该函数,javascript,jquery,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));\"> 已添加
onload=“imgLoaded($(this));”
,当调用该函数时,该函数使图像具有可拖动、可调整大小和可删除的能力
图像:
<img src='data:image/".$ext.";base64,".base64_encode(XXXXX)."' style=\"width:inherit; height:inherit;\" class='img_set' onload=\"imgLoaded($(this));\">
已添加的功能:
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(imgElemt)
{
$('.db_result').html('');
var full_url=document.url;
if(完整url.indexOf('idedit')0){
if(imgElemt.closest(“.child”).width()imgElemt.closest(“.child”).height(){
imgElemt.母公司(“imgh”).身高(“100%”;
imgElemt.parent(“.imgh”).width(imgElemt.width());
}否则{
imgElemt.母公司(“imgh”).宽度(“100%”;
imgElemt.parent(“.imgh”).height(imgElemt.height());
}
}
否则{
imgElemt.parent(“.imgh”).width(imgElemt.width());
}
}
$('.db_result').delay(500).queue(函数(n){$(this.html('');});
$('#liveDimensions').text('Largura:'+imgElemt.width()+'px\nAltura:'+imgElemt.height()+'px');
$('.imgh')。打开('resize',函数(事件,用户界面){
$('#liveDimensions').text('Largura:'+$(this.width()+'px\nAltura:'+$(this.height()+'px');
});
imgElemt.parent('.imgh')。append('');
imgElemt.closest('.child').children('.fileinput holder').remove();
imgElemt.closest('.imgh').draggable({containment:imgElemt.closest('.child'),滚动:true,捕捉:true,捕捉容差:5});
imgElemt.closest('.imgh')。可调整大小({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个不同的图像(在您的情况下,这似乎无论如何都不起作用)