Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery加载事件和我的代码_Javascript_Jquery_Html - Fatal编程技术网

Javascript jQuery加载事件和我的代码

Javascript jQuery加载事件和我的代码,javascript,jquery,html,Javascript,Jquery,Html,好的,我有一些jquery javascript,我打算在其中添加一个.load()事件,希望有人能帮助我 $(function() { $('#nav li a img').each(function() { var originalSrc = this.src, hoverSrc = originalSrc.replace(/\.(gif|png|jpe?g)$/, '_over.$1'); image = new Image(); imag

好的,我有一些jquery javascript,我打算在其中添加一个.load()事件,希望有人能帮助我

$(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;
   });
  });
})

在设置
图像
src
属性之前,请按如下方式附加回调

image.onload = function() {
  // The image has loaded successfully.
}
当映像加载成功后,它将调用该函数

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


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

您看过文档了吗?这里有一些例子:是的,别担心这是alex和我之间的事。说这不公平,因为堆栈溢出问题可以由任何人评论/回答@Felix我在之前的问题中已经对@Jason帮助他进行jQuery和图像加载进行了一些评论。@alex,@Jason:不用担心:)做得好@alex@alex我明白我只是想不出怎么解释你做得很好谢谢你@alex@felix@亚历克斯,我也为你提出了最后一个问题,我希望你能帮助我,你是否在我发布后编辑了我的帖子?事实上,我在编辑之后就做了。那么这在我/你的代码中是什么呢?正确吗?我只需将前面的代码放在{}@Jason中,在设置
src
属性之前,但在实例化
image
之后放置它。因此,将其放在
image.src=hoverSrc之前行。刚才正在阅读有关回调的内容,所以我现在有点明白了:)