javascript - $ ("<img/>").attr ("src",something).load() 在 IE < 9 中不支持?

标签 javascript jquery internet-explorer

http://jsfiddle.net/DerekL/qDqZF/

$('<img/>').attr('src', "http://derek1906.site50.net/navbar/images/pic3.png").load(function() {
    $("body").html("done");
   blah blah blah...
})

我已经使用 $("<img/>").load 进行了测试在 IE 7 中,我得到的是这些:

当我接受律师咨询时,我得到了这个:

"Unable to get value of the property 'attr': object is null or undefined"

当在我的网页中使用时,我得到:

"SCRIPT5007: Unable to get value of the property 'slice': object is null or undefined"
jquery.js, line 2 character 32039

发生了什么? (讨厌IE...)

最佳答案

确保加载函数正在执行。我最近处理了这个问题。在 IE 中,加载函数不会在缓存的图像上触发。出于我的目的,我能够通过不允许图像缓存来解决这个问题。 (一个丑陋的解决方案)

例如:

src="loremIpsum.jpg"

更改为:

src="loremIpsum.jpg?nocache=" + new Date().getTime()

其中“nocache”可以更改为对您有意义的任何内容。

来自 jQuery 文档:

Caveats of the load event when used with images

A common challenge developers attempt to solve using the .load() shortcut is to execute a function when an image (or collection of images) have completely loaded. There are several known caveats with this that should be noted. These are:

  • It doesn't work consistently nor reliably cross-browser
  • It doesn't fire correctly in WebKit if the image src is set to the same src as before
  • It doesn't correctly bubble up the DOM tree
  • Can cease to fire for images that already live in the browser's cache"
    http://api.jquery.com/load-event/

关于javascript - $ ("<img/>").attr ("src",something).load() 在 IE < 9 中不支持?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9644199/

相关文章:

javascript - 为什么我的 AngularJS 动画不起作用?

javascript - Chrome 实验性扩展程序出错?

java - 如何将jsp页面中显示的列表值传递到java中的javascript方法中

javascript - 使用 jQuery 无法动态添加多个元素

javascript - CSS 转换在 IE-11 中不起作用

javascript - 在 JavaScript 中,在不同线程中使用不同参数运行相同的函数

javascript - 如何检测用户使用的是手机、平板电脑还是台式机并重定向他们?

jquery - Fullcalendar:在上一次/下一次点击后保留事件属性

javascript - 如何在 ionic 3 中包含 jquery 插件

php - IE9L自动将文档模式更改为IE7