想要在单击删除链接时删除添加到缩略图中的图像

Want to delete the images being added to the thumbnails when clicked on the delete link

本文关键字:删除 略图 图像 单击 链接 添加      更新时间:2023-09-26

图像被添加为缩略图,但删除操作的代码无法工作。这是我的代码
"span.innerHtml"中出现了问题,我知道。。。但无法解决
将删除操作作为delete as链接的脚本运行良好。。。。但当与图像集成为缩略图时,它无法工作。。。。

//select the files to be added
<input type="file" id="files" name="files[]"/>
<output id="list"></output>
//code for delete operation(after onClick)
<script>
$(document).ready(function(){ 
    $('a.delete').on('click',function(e){
        e.preventDefault();
        imageID = $(this).closest('.image')[0].id;
        alert('Now deleting "'+imageID+'"');
        $(this).closest('.image')
            .fadeTo(300,0,function(){
                $(this)
                    .animate({width:0},200,function(){
                        $(this)
                            .remove();
                    });
            });
    });
}); 
</script>

//code to add images to thumbnails(before onClick)
<script>
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;
        }

        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 = ['<a href="#" class="delete">Delete<img class="thumb" src="', e.target.result,
                    '" title="', escape(theFile.name), '" width="110" height="150"/></a>'].join('');
                document.getElementById('list').insertBefore(span, null);
            };
        })(f);
        // Read in the image file as a data URL.
        reader.readAsDataURL(f);
    }
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>  

如果它以某种方式工作,并且不适用于动态添加的图像,则应该更改以下内容:

 $('a.delete').on('click',function(e){...

 $(document).on('click','a.delete', function(e){...

这个选择器也不正确,因为你没有名为"image"的css类:

$(this).closest('.image')

它应该是(将其更改为查找,因为它是一个子元素):

$(this).find('img')

否则,请分享一些细节,它是如何不起作用的。有错误吗?

在修复了我写的所有内容后,它就开始工作了(所以谢谢你的减号):http://jsfiddle.net/balintbako/EdkHN/

此外,您可能还想删除整个跨度:http://jsfiddle.net/balintbako/EdkHN/1/