设置 img src 属性时宽度和高度不变
使用 Javascript 更改 src... 第一次正确设置宽度和高度属性。第二次他们没有改变。为什么?
所以我的页面上有这个空白图像...
因此,每次用户上传图像时,上传者的 UploadComplete 回调都会执行将此图像的src设置为刚刚上传的图像,以便可以对其进行裁剪。然后,我使用该图像的高度和宽度属性来创建放大的裁剪预览。
第一次效果很好。浏览器或 DOM 或某些东西会自动设置 img 的高度和宽度属性,我在图像加载后通过 DOM 访问这些属性。很美丽。
如果用户上传了错误的图像,我想允许他们上传不同的图像。然而这一次,通过 javascript 访问的 img 的宽度和高度属性仍然与之前的图像相同。新图像以其自己的宽度和高度正确显示,只是标签的属性不会随着新的 src 而改变。
有谁知道为什么会发生这种情况?或者更好的是,我如何在加载新的 src 时确定性地获取 img 的高度和宽度?
我 <3 stackoverflow,我 <3 你。
Using Javascript to change an 's src... First time the width and height properties are being set correctly. The Second time they don't change. Why?
So I've got this blank image on my page... <img id="imgCropImage" />
So the idea is that every time a user uploads an image, the uploader's UploadComplete callback will set this image's src to the image that has just been uploaded so that it can be cropped. I then use this image's height and width properties in order to create a blown up crop preview.
It works great the first time. The browser or the DOM or something automagically set the img's height and width attributes which I access via the DOM after the image has loaded. It's beautiful.
If the user uploaded the wrong image, I want to allow them to upload a different one. This time however, the img's width and height attributes, accessed via javascript, remain the same as the prior image. The new image displays correctly in it's own width and height, it's just that the tag's properties do not change along with a new src.
Does anyone know why this happens? Or even better, how I could deterministically get the img's height and width upon loading a new src?
I <3 stackoverflow and I <3 you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“删除”高度和宽度属性怎么样?
那应该“重置”它。 (如果您使用的是 JQuery)。
What about "removing" the height and width attributes.
That should "Reset" it. (If you're using JQuery that is).
您是否尝试使用新的 src 用新的图像替换以前的图像?
Did you try to replace the previous image by a new one, with the new src?