jQuery图像加载回调 [英] jQuery image load callback
问题描述
在载入之后动态地向页面添加图像,然后进行用户交互,并且在图像完全加载后无法启动该函数。我想,使用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屋!