使用jQuery为图像源添加缓存前缀

时间:2014-02-04 13:20:54

标签: javascript jquery caching

我正在尝试为图片添加一些无缓存前缀:

var cacheId = Math.floor(Math.random() * 10000) + 1;
$(this).attr('src', src + cacheId);

但我有一个错误:src is not defined,出了什么问题?

1 个答案:

答案 0 :(得分:1)

您的名为src的变量目前尚未定义。

我相信你想做这样的事情。 Fiddle

$(document).ready(function () {  
    $("img").each(function () {
        var src = $(this).attr('src');
        var cacheId = Math.floor(Math.random() * 10000) + 1;
        $(this).attr('src', src + "?r=" + cacheId);
    });
});