jQuery加载我的代码事件 [英] jQuery load event with my code

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

问题描述

好吧,我有一些jQuery的JavaScript,我打算添加一个.load()事件,希望有人可以帮助我。

 <$ c $ (函数(){
var originalSrc = this.src,
hoverSrc = $ s $,$($#
$ b $('#nav li a img' originalSrc.replace(/ \。(gif | png | jpe?g)$ /,'_over。$ 1');
image = new Image();

image.src = hoverSrc;

$(this).hover(function(){
this.src = hoverSrc;
},function(){
this.src = originalSrc ;
});
});
})


解决方案

在设置 Image src 属性之前,所以...

  image.onload = function(){
//图像已成功加载。
}

当图片加载并成功时,它会调用该函数。 / p>

如果函数不成功,它将调用分配给 onerror 属性的函数。



或者,您可以使用 $(image).load(function(){...})


Ok i have some jquery javascript which i intend on adding a .load() event to, hopefully someone can assist me.

$(function() {

  $('#nav li a img').each(function() {
   var originalSrc = this.src,
       hoverSrc = originalSrc.replace(/\.(gif|png|jpe?g)$/, '_over.$1'); 
       image = new Image();

   image.src = hoverSrc;

   $(this).hover(function() {
      this.src = hoverSrc;
   }, function() {
      this.src = originalSrc;
   });
  });
})

解决方案

Before you set the src property of the Image, attach the callback like so...

image.onload = function() {
  // The image has loaded successfully.
}

When the image has loaded and is successful, it will call that function.

If the function is not successful, it will call the function assigned to the onerror property.

Alternatively, you can use $(image).load(function() { ... }).

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

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