使用按钮刷新任意图像
Refresh an arbitrary image with a button
>我有一个自动生成的表单,它也很容易显示缩略图。我想在图像旁边添加一个按钮,如果更新,它将刷新它。我想考虑缓存的图像。
我很容易添加一个按钮:
// add a refresh button to image previews
$('a[href*="/thumbs/"]').after('<div class="refresh-image"><button class="refresh-image-button">Refresh</button></div>');
由于如果刷新按钮刷新所有过时的图像,我可以接受,因此我尝试将查询字符串附加到图像 src(如果存在,请删除旧的查询字符串):
// attach refresher to button
$(".refresh-image-button").click(function() {
$('img[src*="/thumbs/"]').attr("src", $(this).attr("src").split('?',1)+'?'+Date.now());
});
但是,我真的不能这样做,因为这里的 $(this) 不引用匹配的图像,无论如何我都无法在此 jquery 匹配中执行拆分操作。
我该怎么做?
作为奖励,如果有人有体面的自动刷新图像代码的参考,那将很有帮助。
这基于此处更简单的SO查询。
遍历集合:
$(".refresh-image-button").click(function() {
$('img[src*="/thumbs/"]').each(function() {
$(this).attr("src", $(this).attr("src").split('?',1)+'?'+Date.now());
});
});
http://jsfiddle.net/g68gxu93/1/
您可以使用新的尾随参数单独生成一个变量,例如:
$(".refresh-image-button").click(function() {
var newAttr = $('img[src*="/thumbs/"]').attr("src").split("?")[0] + "?ver=" + new Date().getTime();
$('img[src*="/thumbs/"]').attr("src", newAttr);
});
参见 JSFIDDLE
我最终做了一些几乎完全像@Moogs答案的事情。
addRefreshButtons();
// NICEITIES
function addRefreshButtons() {
// add a refresh button to image previews
$("div.refresh-image").remove();
$('a[href*="/thumbs/"]').after('<div class="refresh-image"><button class="refresh-image-button">Refresh</button></div>');
// attach refresher to button
$(".refresh-image-button").click(function() {
refreshThumbnails();
});
}
function refreshThumbnails() {
$('img[src*="/thumbs/"]').each(function() {
var src = $(this).attr('src');
var newSrc = src.split('?',1)+'?'+Date.now()
$(this).attr('src', newSrc);
});
}
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- nodejs-expressjs上传图像并显示它们
- 使用JSP从服务器检索和显示图像
- 角度:在ng重复上切换图像
- 画廊图像未显示
- Phonegap-(安卓/iphone)多个图像的图像库出现问题
- Javascript排序的图像弹出窗口..可以't单独弹出
- 画布数据到图像
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 使图像在单击时展开到不大于浏览器
- Html页面上的多个Base64图像和平滑加载
- UIAutomation放大图像
- 使用javascript在Flash中加载外部图像
- 可以在这里为背景图像设置滤镜吗
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 使用按钮刷新任意图像
- 如何将任意数量的图像动态添加到源字符串位于数据库中的 Web 窗体
- 在固定大小的块内显示具有圆角的任意大小的图像
- 如何加载任意数量的图像到画布上
- {"error ":["无效的图像URL"]}与任意集成