IE中不支持$("< img />")。attr(" src",something).load()< 9? [英] $("<img/>").attr("src",something).load() not supported in IE < 9?
问题描述
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 />)进行了测试。 / code>在IE 7中,我得到的是这些:
There I have tested using $("<img/>").load
in IE 7, and what I got are these:
当在律师中运行时,我得到这个:
When run in counsel, I get this:
无法获取属性'attr'的值:object为null或undefined
"Unable to get value of the property 'attr': object is null or undefined"
在我的网页上使用时,我得到这个:
When used in my webpage, I get this:
SCRIPT5007:无法获取属性'slice'的值: object为null或undefined
jquery.js,line 2 character 32039
"SCRIPT5007: Unable to get value of the property 'slice': object is null or undefined"
jquery.js, line 2 character 32039
发生了什么? (讨厌IE ...)
What happened? (Hate IE...)
推荐答案
确保正在执行加载功能。我最近处理过这个问题。在IE中,加载功能未在缓存的图像上触发。出于我的目的,我能够通过永远不允许图像缓存来解决这个问题。 (一个丑陋的解决方案)
Ensure that the load function is being executed. I recently dealt with this issue. In IE the load function wasn't firing on cached images. For my purposes I was able to get around this by never allowing the image to cache. ( An ugly solution )
ex:
src="loremIpsum.jpg"
更改为:
src="loremIpsum.jpg?nocache=" + new Date().getTime()
其中nocache可以更改为对你有意义的任何内容。
Where "nocache" can be changed to anything that makes sense to you.
来自jQuery文档:
From the jQuery documentaion:
与图像一起使用时加载事件的注意事项
Caveats of the load event when used with images
开发人员尝试使用<$ c解决的常见挑战$ c> .load()快捷方式是在图像(或图像集合)完全加载时执行函数。应该注意有几个已知的警告。这些是:
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:
- 它不能始终如一地工作,也不能可靠地跨浏览器
- 它没有'如果图像src设置为与之前相同的src,则在WebKit中正确触发
- 它没有正确地冒泡DOM树
- 可以停止播放已存在于浏览器缓存中的图片
http:// api.jquery.com/load-event/
- 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/
这篇关于IE中不支持$("< img />")。attr(" src",something).load()< 9?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!