JavaScript onload函数未启动
让我解释一下我想做什么。我已经开发了一个web应用程序,在运行动画之前,我希望确保动画中使用的图像已加载到浏览器中。因此,以下是我的JavaScript:JavaScript onload函数未启动,javascript,jquery,Javascript,Jquery,让我解释一下我想做什么。我已经开发了一个web应用程序,在运行动画之前,我希望确保动画中使用的图像已加载到浏览器中。因此,以下是我的JavaScript: $(document).ready(function(){ $.mobile.loading( 'show', { text: 'Downloading Web App', textVisible: tru
$(document).ready(function(){
$.mobile.loading( 'show', {
text: 'Downloading Web App',
textVisible: true,
theme: 'a',
html: ""});
document.getElementById('Logo').onload = function()
{
if(KeepCount == 0)
{
KeepCount++;
}
else if(KeepCount == 1)
{
KeepCount = 0;
$.mobile.loading( 'hide' );
StartSplash();
}
};
document.getElementById('Hand').onload = function(){
if(KeepCount == 0)
{
KeepCount++;
}
else if(KeepCount == 1)
{
KeepCount = 0;
$.mobile.loading( 'hide' );
StartSplash();
}
};
});
KeepCount是全局声明的。我在chromes developer tools中检查过这一点,不知怎么的,由于某种原因,onload函数从未启动过。以下是HTML:
<img id='Logo' class="logo objecttransition" src="css/images/Logo.gif">
<img id='Hand' class="hand objecttransition" src="css/images/hand.gif">
有什么想法吗?如果你在.ready范围内声明这一点,我想可能有点晚了 也许通过javascript加载你的图像,然后你就可以确定它们已经准备好了
function startLoadImages(){
var image = new Image();
image.load = function(){
// i am done, inject me !!
}
image.src = "css/images/Logo.gif"
}
$(document).ready(function(){
startLoadImages();
});
我必须同意乔纳森的观点。位于
.ready
处理程序的jQuery
文档中,说明以下内容
…而JavaScript提供加载事件,用于在
页面被呈现时,直到所有资源
如图片已全部收到
.ready()方法是
通常与属性不兼容。如果加载
必须使用,请不要使用.ready()或使用jQuery的.load()
方法将加载事件处理程序附加到窗口或更特定的
项目,如图像
如果要确保先加载图像,则可能需要使用
$(window).load(function()
{
...
});
在jquery的
$.ready
函数中,很可能在分配onload事件时图像已经加载。如果已加载图像,则不会触发onload。