简体   繁体   English

在ajax的成功函数中重新加载html文件的div

[英]Reload a div of html file in the success function of ajax

I want to display the image right after uploading it.For this I want to reload the div with id="imagecontainer" in the success function of ajax. 我想在上载后立即显示图像。为此,我想在ajax的成功函数中使用id =“ imagecontainer”重新加载div。

Please do not tell about using ("$id").load("href") function as it does not work in firefox and is very clunky on chrome. 请不要告诉使用(“ $ id”)。load(“ href”)函数,因为它在Firefox中不起作用,并且在chrome上非常笨拙。

Any ideas how to achieve this? 任何想法如何实现这一目标?

So you want to reload the image, but it has the same url? 因此,您想重新加载图像,但是它具有相同的URL? You can force the image to load anew by appending a random url parameter. 您可以通过添加随机url参数来强制重新加载图片。 That way, the browser thinks it is a new resource, but the server will simply ignore the url parameter. 这样,浏览器认为它是一种新资源,但是服务器将只忽略url参数。

var $img = $("#imagecontainer");
$img.attr("src", $img.attr("src")+"?"+(new Date()).getTime());
//  my/path/to/image.jpg  ->  my/path/to/image.jpg?1401742135778

Using (new Date()).getTime() is better than Math.random() because the same time won't ever occur twice. 使用(new Date()).getTime()Math.random()更好,因为同一时间永远不会出现两次。 This method is also known as cache busting 此方法也称为缓存清除

Reload the src attribute of the image. 重新加载图像的src属性。 $("#imgsrc").attr("src", path); where path is your image's url path. 其中path是图片的网址路径。

您可以像这样直接修改图像的来源

$('#imgsrc').attr('src', data.url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM