为每个图像缩略图预览添加删除按钮
Adding delete button for each image thumbnail preview
我想在每个缩略图旁边放一个小叉,以便能够逐个删除它们。我能够通过点击它来删除图像,但我想有单独的按钮"x"来删除它。
fileInput.addEventListener("change", function (e) {
var filesVAR = this.files;
showThumbnail(filesVAR);
}, false);
function showThumbnail(files) {
var file = files[0]
var image = document.createElement("img");
var thumbnail = document.getElementById("thumbnail");
image.file = file;
image.setAttribute('class', 'imgKLIK5');
thumbnail.appendChild(image)
var reader = new FileReader()
reader.onload = (function (aImg) {
return function (e) {
aImg.src = e.target.result;
};
}(image))
var ret = reader.readAsDataURL(file);
var canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
image.onload = function () {
ctx.drawImage(image, 100, 100);
}
}
});
下面是我在JFIDDLE
中的代码你能帮我一下吗?查看关闭按钮的示例:http://jsfiddle.net/kevalbhatt18/r0taz01L/1/
<我>我>
$('div').on('click', '.closeDiv', function () {
$(this).prev().remove();
$(this).remove();
$('#upload-file').val("");
});
我在你的showThumbnail函数
中做了这些修改<我>我>
function showThumbnail(files) {
var file = files[0]
var thumbnail = document.getElementById("thumbnail");
var pDiv = document.createElement("div");
var image = document.createElement("img");
var div = document.createElement("div");
pDiv.setAttribute('class', 'pDiv');
thumbnail.appendChild(pDiv);
image.setAttribute('class', 'imgKLIK5');
pDiv.appendChild(image)
div.innerHTML = "X";
div.setAttribute('class', 'closeDiv');
pDiv.appendChild(div).......
...................
只需在每个预览中添加新的button元素,并使用以下样式属性,
float:right;
position:relative;
您可以将img
从div thumbnail
中移除,点击"x"按钮,查看此页面
相关文章:
- JS动态添加字段-删除按钮不起作用
- 如何根据某些条件向可编辑的jquery数据表添加或删除按钮
- 克隆字段上的“添加-删除”按钮
- 删除jQuery添加的按钮
- 单击单选按钮,添加/删除表单元素的类
- 使用 jquery 在函数中添加删除按钮
- 如何在使用Angular.js打开新行后添加删除按钮
- 添加删除圆圈按钮
- 添加/删除基于所选单选按钮的下拉选项
- Jquery:如何将图像添加到删除按钮
- 单击按钮时添加/删除类
- jquery JavaScript 添加/删除按钮
- 从按钮添加/删除类
- 使用KnockoutJS在添加/删除按钮之间切换
- 应用程序有错误.意外的标记."尝试添加删除按钮
- 为每个图像缩略图预览添加删除按钮
- 如何在jQuery的clone()方法中为新添加的表行添加删除按钮
- 如何在列表中添加删除按钮
- 如何在每行添加删除按钮
- 为附加元素添加删除按钮