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/