Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何刷新<;img/>;用jQuery?_Jquery - Fatal编程技术网

如何刷新<;img/>;用jQuery?

如何刷新<;img/>;用jQuery?,jquery,Jquery,但它不起作用。添加时间戳或随机数: $('#verifyimage').click(function() { $(this).attr('src',$(this).attr('src')); }); 您可以通过在末尾附加随机字符串来强制刷新,从而更改URL: var timestamp = new Date().getTime(); $(this).attr('src',$(this).attr('src') + '?' +timestamp ); 在test.php

但它不起作用。

添加时间戳或随机数:

$('#verifyimage').click(function() {
        $(this).attr('src',$(this).attr('src'));
    });

您可以通过在末尾附加随机字符串来强制刷新,从而更改URL:

var timestamp = new Date().getTime();
$(this).attr('src',$(this).attr('src') + '?' +timestamp );

在test.php中,将
内容类型:
的标题设置为
图像/jpeg

获取KPrimes的精彩答案并添加跟踪器建议,这就是我想到的:

$('#verifyimage').click(function() {
    $(this).attr('src', $(this).attr('src')+'?'+Math.random());
});

哎哟。不,不要。改为在服务器上设置适当的缓存响应头。
缓存控制
过期
,等等。只有在收到HTTP请求后才能工作-以上只是强制客户端发出该请求。在没有
Image.reload
(AFAIK)的情况下,这是创建新请求的最简单的方法。它建议将src属性上的子字符串添加到“?”中,这样在第一次之后,它就不会继续添加更多的查询字符串数据……这与jeerose在这个线程中的回答相同:@KPrime it's great!!我费了好几个小时才让它工作起来。谢谢时间戳是一个更好的解决方案!你不能依赖随机有时不一样。尝试过这个,但在chrome上有问题。图像src会附加时间戳,但缩略图不会刷新。你能给我一些建议吗?@RohanPatil-这里有一个明显的问题-你想添加或更新一个查询参数-这段代码只是一个部分解决方案-它假设没有查询参数开始(此外,我怀疑它只工作一次)。
$('#verifyimage').click(function() {
    $(this).attr('src', $(this).attr('src')+'?'+Math.random());
});
jQuery(function($) {
    // we have both a image and a refresh image, used for captcha
    $('#refresh,#captcha_img').click(function() {
       src = $('#captcha_img').attr('src');
   // check for existing ? and remove if found
       queryPos = src.indexOf('?');
       if(queryPos != -1) {
          src = src.substring(0, queryPos);
       }    
       $('#captcha_img').attr('src', src + '?' + Math.random());
       return false;
    });
});