在IE中的onload和onerror事件处理程序中确定图像src [英] Determine image src in onload and onerror event handlers in IE

查看:76
本文介绍了在IE中的onload和onerror事件处理程序中确定图像src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定IE中onload和onerror事件处理程序中触发事件的图像的图像src?我将以下示例代码放在一起:

How can I determine the image src of the image that triggered the event in the onload and onerror event handlers in IE? This example code I threw together:

<script language="javascript" type="text/javascript" src="jquery.js"></script>
<script language="javascript" type="text/javascript">
function loadImages() {
   var goodImage = new Image();
   var missingImage = new Image();

   $(goodImage).bind('load', function(event){
      $("#log").append( $(event.target).attr('src') + ' WAS FOUND <br>');
   });

   $(missingImage).bind('load', function(event){
      $("#log").append( $(event.target).attr('src') + ' WAS FOUND <br>' );
   });

   $(goodImage).bind('error', function(event){
      $("#log").append( $(event.target).attr('src') + ' IS MISSING <br>');
   });

   $(missingImage).bind('error', function(event){
      $("#log").append( $(event.target).attr('src') + ' IS MISSING <br>');
   });

  goodImage.src = 'GOOD-IMAGE.GIF'; // this image exists
  missingImage.src = 'MISSING-IMAGE.GIF'; // this image doesn't exist
}

</script>
</head>
<body onload="loadImages();">
<div id="log"></div>

在FF中工作,但在IE8中,它为$(event.target).attr('src')部分打印出未定义的内容.我以为jQuery应该规范IE的事件对象,使其像其他浏览器一样工作?我已经尝试了多种排列方式,但是无法在IE8中使用任何功能.

works in FF but in IE8 it prints out undefined for the $(event.target).attr('src') part. I thought jQuery was supposed to normalize the event object for IE so that it acted like other browsers? I've tried a number of permutations but haven't been able to get anything to work in IE8.

无论如何,如果有人对如何在IE中工作的onload和onerror事件处理程序中找出图像src有任何建议,我将非常感谢.甚至甚至如何在加载了图像后又未加载的图像中弄清楚(但不是图形方式-我需要生成一个包含未加载图像文件名的数组).谢谢!

Anyway if anyone has a suggestion on how to figure out the image src in the onload and onerror event handlers that works in IE I would really appreciate it. Or even how to figure out after the images have loaded which have loaded and which haven't (but not graphically - I need to generate an array containing the filenames of the images that didn't load). Thanks!

推荐答案

在Firefox和所有好的浏览器中,它都是.target;在IE中,它是.srcElement.只需在每次追加之前插入以下行:

In Firefox and all good browsers it's .target; in IE it's .srcElement. Just insert this line before each append:

var targetEl = event.target || event.srcElement;

然后将event.target更改为:

targetEl

这篇关于在IE中的onload和onerror事件处理程序中确定图像src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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