$(this)和scope - 传递特定元素

pan*_*hro 0 javascript jquery

我有各种各样的.img-drop-zones文件读取,我希望将它显示在它所放置的特定放置区域的顶部,但我如何得到它?$(this)由于范围不起作用,我该如何通过?

 $('.img-drop-zone').on('drop', function(e){

        var files = e.originalEvent.dataTransfer.files;

        $.each(files, function(index, file){

            p.file.read(file, function(content) {
                //how can I get the img-drop zone here?

            });

        }) 


    });
Run Code Online (Sandbox Code Playgroud)

Aln*_*tak 5

只需在外部作用域中声明一个可以在$.each闭包中引用的附加变量:

var $this = $(this);

$.each(..., function() {
    // use $this here to refer to the img-drop-zone
});
Run Code Online (Sandbox Code Playgroud)

当将对象引用到不是jQuery对象的东西时,使用self或更常见that.