1
這是我動態創建的圖像:如何在下面的代碼中刪除動態創建的圖像?
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
fileNames.push(f.name);
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
var span = document.createElement('span');
span.innerHTML = ['<img class="thumb" id="',escape(theFile.name),'" src="',e.target.result,
'" title="', escape(theFile.name), '"/><input class="delete" type="button" value="delete" name="',escape(theFile.name),'"/>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
而這正是我想要刪除的圖像時,我按下按鈕:
$('#list').on("click",".delete",function(e){
$(e.target).remove();
});
還可以引用在'' id來執行'除去()'函數。 – Raptor 2013-05-13 07:14:47
由於OP使用'.delete'來附加事件處理程序 - 圖像可以用'$(this)'引用。或者如果他不想刪除id _as你說_你需要先獲得圖像的ID'$(this).prev('img')。attr('id');' – 2013-05-13 07:20:28
是的,你是對! – Raptor 2013-05-13 07:21:01