jQuery图像加载回调 [英] jQuery image load callback

查看:143
本文介绍了jQuery图像加载回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在载入之后动态地向页面添加图像,然后进行用户交互,并且在图像完全加载后无法启动该函数。我想,使用jQuery的 .load()方法会很好..但根据我的控制台和一堆文档 .log 业务从未执行。见下文..谢谢!

Dynamically adding an image to the page after load then user interaction and am having trouble firing a function after that image has completely loaded. I figured that using jQuery's .load() method would be fine.. but according my console and a bunch of documenting .log business it's never executed. See below.. thank you!

$('body')
    .append('<img id="ad" src="img/ad.jpg" alt="Advertising Network" />')
    .load(function(){
        console.log('ad load complete');
        $('#submit').click();
    });






UPDATE:


UPDATE:

要澄清,这是在文档窗口加载后。

To clarify, this is after both document and window load.

推荐答案

在您的代码 .load()

In your code .load() is related to the body because of chaining.

将代码更改为:

var img = $('<img id="ad" src="img/ad.jpg" alt="Advertising Network" />');
img.load(function(){
    console.log('ad load complete');
    $('#submit').click();
});

$('body').append(img);

,它应该可以正常工作(http://jsfiddle.net/zerkms/f7epb/

and it should work (http://jsfiddle.net/zerkms/f7epb/)

这篇关于jQuery图像加载回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆