jQuery插件参数中的$(this)选择器指针

$(this) selector pointer in jQuery plugin parameters

本文关键字:this 选择器 指针 插件 参数 jQuery      更新时间:2023-09-26

对于许多选择器设置选项:

$('#fileupload1, #fileupload2, #fileupload3').dropZone(
    'option',
    {
        url: '/path/to/upload/handler.json',
        dropZone: $(this).find('.dropzone') //Does not work!!!
    }
);

为什么?请帮助!

因为this没有引用选择器中的元素

var $els = $('#fileupload1, #fileupload2, #fileupload3');
$els.dropZone('option', {
    url: '/path/to/upload/handler.json',
    dropZone: $els.find('.dropzone') //Does not work!!!
});

如果您想将单个dropzone传递给所有3个元素,则使用each循环

$('#fileupload1, #fileupload2, #fileupload3').each(function () {
    $(this).dropZone('option', {
        url: '/path/to/upload/handler.json',
        dropZone: $(this).find(.'dropzone') //Does not work!!!
    });
})