img onload 在 IE7 中无法正常工作 [英] img onload doesn't work well in IE7
问题描述
我的 web 应用中有一个 img 标签,它使用 onload 处理程序来调整图像大小:
I have an img tag in my webapp that uses the onload handler to resize the image:
<img onLoad="SizeImage(this);" src="foo" >
这在 Firefox 3 中工作正常,但在 IE7 中失败,因为由于某种原因传递给 SizeImage()
函数的图像对象的宽度和高度为 0 -- 也许 IE 调用了该函数在它完成加载之前?.在研究这一点时,我发现其他人在使用 IE 时也遇到了同样的问题.我还发现这不是有效的 HTML 4.这是我们的 doctype,所以我不知道它是否有效:
This works fine in Firefox 3, but fails in IE7 because the image object being passed to the SizeImage()
function has a width and height of 0 for some reason -- maybe IE calls the function before it finishes loading?. In researching this, I have discovered that other people have had this same problem with IE. I have also discovered that this isn't valid HTML 4. This is our doctype, so I don't know if it's valid or not:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
是否有一种合理的解决方案可以在加载图像时调整其大小,最好是符合标准的解决方案?该图像用于用户上传他们自己的照片,几乎可以是任何尺寸,我们希望以最大 150x150 显示它.如果您的解决方案是在上传时调整图像服务器端的大小,我知道这是正确的解决方案,但我被禁止实施它:( 必须在客户端完成,并且必须在显示上完成.
Is there a reasonable solution for resizing an image as it is loaded, preferably one that is standards-compliant? The image is being used for the user to upload a photo of themselves, which can be nearly any size, and we want to display it at a maximum of 150x150. If your solution is to resize the image server-side on upload, I know that is the correct solution, but I am forbidden from implementing it :( It must be done client side, and it must be done on display.
谢谢.
编辑:由于我们应用程序的结构,在文档的 onload 中运行此脚本是不切实际的(几乎不可能).我只能合理地编辑图像标签和它附近的代码(例如,我可以在它的正下方添加一个 ).此外,我们已经有了 Prototype 和 EXT JS 库……管理层宁愿不必添加另一个库(有些答案建议使用 jQuery).如果使用这些框架可以解决这个问题,那就太好了.
Edit: Due to the structure of our app, it is impractical (bordering on impossible) to run this script in the document's onload. I can only reasonably edit the image tag and the code near it (for instance I could add a <script>
right below it). Also, we already have Prototype and EXT JS libraries... management would prefer to not have to add another (some answers have suggested jQuery). If this can be solved using those frameworks, that would be great.
编辑 2:不幸的是,我们必须支持 Firefox 3、IE 6 和 IE 7.也希望支持所有基于 Webkit 的浏览器,但因为我们的网站目前不支持它们,我们可以容忍仅适用于三大巨头的解决方案.
Edit 2: Unfortunately, we must support Firefox 3, IE 6 and IE 7. It is desirable to support all Webkit-based browsers as well, but as our site doesn't currently support them, we can tolerate solutions that only work in the Big 3.
推荐答案
IE7 试图在 DOM 树完全呈现之前调整图像大小.您需要在 document.onload 上运行它...您只需要确保您的函数可以处理传递对不是this"的元素的引用.
IE7 is trying to resize the image before the DOM tree is fully rendered. You need to run it on document.onload... you'll just need to make sure your function can handle being passed a reference to the element that isn't "this."
或者……我希望这不是一个可恶的冒犯……jQuery 让这样的事情变得非常非常简单.
Alternatively... and I hope this isn't a flameable offense... jQuery makes stuff like this really, really easy.
根据编辑 1 进行
您可以将 document.onload(runFunction);
放在任何脚本标记中,在正文的任何位置.它仍然会等到文档加载后运行该函数.
You can put document.onload(runFunction);
in any script tag, anywhere in the body. it will still wait until the document is loaded to run the function.
这篇关于img onload 在 IE7 中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!