jquery检测特定(外部)图像何时加载

时间:2013-12-23 10:14:38

标签: jquery iframe

当加载带有youtube剪辑的iframe时,来自youtube的最后一个文件就是这张图片:

http://i1.ytimg.com/vi/W6YOAlxT7Ls/hqdefault.jpg

我想在加载特定图像时触发一个函数。我该如何检测? 这个w / jquery?

我试过这个:

$('<img src="http://i1.ytimg.com/vi/W6YOAlxT7Ls/hqdefault.jpg"/>').on('load', function() { 
    // bam
});

...但它会导致重新加载相同的图像。我不需要获取图像,我只需要知道何时获取图像。

基督。即使这样也会导致重新获取图像:

$.when($('<img src="http://i1.ytimg.com/vi/W6YOAlxT7Ls/hqdefault.jpg"/>').load()).then( bam() );

1 个答案:

答案 0 :(得分:1)

试试这个

$('<img src="http://i1.ytimg.com/vi/W6YOAlxT7Ls/hqdefault.jpg"/>').one('load', function () {
        //your code to fire when image has loaded 
    }).each(function () {
        if (this.complete) $(this).load();
    });