jquery代码在IE8或更早版本中无效

时间:2012-05-24 11:14:04

标签: jquery internet-explorer

有人能告诉我为什么这段代码在IE8或更老版本中不起作用吗? 它不会抛出错误,只是似乎没有运行。

$('<img/>')
.attr('src', $('.my_div img:first').attr('src'))
.load(function(){
   console.log('hello');
       alert('hello');
}); 

感谢。

1 个答案:

答案 0 :(得分:1)

在IE8中,加载事件在您设置事件处理程序之前发生。在设置源属性之前设置load事件:

$('<img/>').load(function(){
  console.log('hello');
}).attr('src', $('.my_div img:first').attr('src'));

演示:http://jsfiddle.net/Guffa/GRKJE/

相关:load event with jquery fails in IE 8

相关问题