如何在jQuery中重新加载/刷新元素(图像) [英] How to reload/refresh an element(image) in jQuery
问题描述
是否可以使用jQuery从服务器重新加载具有相同文件名的图像?
Is it possible to reload an image with an identical file name from a server using jQuery?
例如,我在页面上有一幅图像,但是,物理图像可以根据用户操作而改变.请注意,这并不意味着文件名更改,而是实际的文件本身.
For example, I have an image on a page, however, the physical image can change based on user actions. Note, this does not mean the file name changes, but the actual file itself.
即:
- 用户在默认页面上查看图像
- 用户上传新图像
- 页面上的默认图像未更改(我认为这是由于文件名相同,浏览器使用了缓存的版本)
无论下面的代码多久被调用一次,相同的问题仍然存在.
Regardless of how often the code below is called, the same issue persists.
$("#myimg").attr("src", "/myimg.jpg");
在jQuery文档中,如果加载"函数具有触发事件的默认方法,而不是将回调函数绑定到元素的成功/完整加载,则该函数将是完美的.
In the jQuery documentation, the "load" function would be perfect if it had a default method of firing the event as opposed to binding a callback function to a successful/complete load of an element.
我们非常感谢您的协助.
Any assistance is greatly appreciated.
推荐答案
听起来好像是您的浏览器缓存图像(我现在注意到您在问题中写的).您可以通过传递一个额外的变量来强制浏览器重新加载图像,如下所示:
It sounds like it's your browser caching the image (which I now notice you wrote in your question). You can force the browser to reload the image by passing an extra variable like so:
d = new Date();
$("#myimg").attr("src", "/myimg.jpg?"+d.getTime());
这篇关于如何在jQuery中重新加载/刷新元素(图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!