Javascript img.on(“加载”)发射次数过多
下面的代码对它应该处理src加载事件的10个映像执行了几乎100次。不完全是我所期望的。我做错了什么Javascript img.on(“加载”)发射次数过多,javascript,jquery,Javascript,Jquery,下面的代码对它应该处理src加载事件的10个映像执行了几乎100次。不完全是我所期望的。我做错了什么 //next line contains a stringof <img> tags wrapped in divs $(".carousel-inner").append(htmlStreamImage); //this next clause is the problem-child $(".carousel-inner div.item img").on("load", fun
//next line contains a stringof <img> tags wrapped in divs
$(".carousel-inner").append(htmlStreamImage);
//this next clause is the problem-child
$(".carousel-inner div.item img").on("load", function () {
//build the indicators list
foo();
console.debug(accumulator++);
if (carouselImagesLoaded == numberPhotos) {
foo2();
}
}).each(function () {
if (this.complete) $(this).load();
});
//下一行包含一系列用div包装的标记
$(“.carousel-inner”).append(htmlStreamImage);
//下一条就是问题孩子
$(“.carousel内部分区项目img”)。打开(“加载”,函数(){
//建立指标清单
foo();
调试(累加器++);
if(旋转木马limagesloaded==numberPhotos){
foo2();
}
}).每个(功能){
如果(this.complete)$(this.load();
});
您可以缩小代码:
$(".carousel-inner").append(
$("<img/>").attr({...}).load(function(){
// building process
})
);
$(“.carousel-inner”).append(
$(“”).attr({…}).load(函数(){
//建造过程
})
);
何时何地为每个元素设置src
。我已经把它包括在编辑中了。如果这很重要,则在.ajax调用的success子句中