为什么?我对图片标签有同样的问题.
调用ready()回调.永远不会调用load()回调.
浏览器:Mac上的Firefox3.6.8
编辑:
我不知何故感觉我在JQuery中错误地使用了load().我所指的文档: - http://api.jquery.com/load-event/
我在做
$("body").load(function(){// do something});
这不对吗?我看到一些代码在做: - $("#id").load("./ newpage.html"); 但这2个是不同的API吗?
编辑2
还有一些代码可以在这里解释我的整个问题: -
var tagString = "<img id='"+imageId+"'></img>";
this.divElem.append(tagString);
var imgElems = $("#"+imageId);
var vowels = this;
imgElems.each(function (index) {
$(this).attr('id',imgId).attr('src',imageUrl)
.attr('width',1).attr('height',1);
$(this).load(function() {
// do something.
// This Damned! function is never getting called!
});
});
Run Code Online (Sandbox Code Playgroud)
作品
$().ready(function() {
$().load(function() {
/// something. this worked!
});
});
Run Code Online (Sandbox Code Playgroud)
不行
// without the ready() wrapper does not work
$().load(function() {
/// something. this worked!
});
Run Code Online (Sandbox Code Playgroud)
为什么?我很高兴它有效.但我不明白为什么!
-Ajay
与您链接到的页面上的示例一样,使用:
$(window).load(function(){
// do something
});
Run Code Online (Sandbox Code Playgroud)
也许可以像你尝试的那样将事件绑定到body元素,但是你必须将代码放在body元素中.如果你把脚本放在头部(脚本最好去的地方),那么当代码运行时,body元素就不存在了.