如何在以下代码中删除动态创建的图像?

时间:2013-05-13 07:10:59

标签: jquery

这是我动态创建图像的地方:

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();

            });

1 个答案:

答案 0 :(得分:2)

因为图片位于您点击的按钮之前

$('#list').on("click",".delete",function(e){
   $(this).prev('img').remove();
});