jQuery,如何检查插件是否已经应用于div?

时间:2011-04-14 21:39:47

标签: jquery jquery-ui jquery-plugins

jQuery文件上传器:https://github.com/blueimp/jQuery-File-Upload

我正在使用上面的插件。如何在jQuery中查看是否已经应用了fileUpload?

我现在收到以下错误:

Uncaught FileUpload with namespace "file_upload" already assigned to this element
jQuery.jQuery.extend._Deferred.deferred.resolveWithjquery-1.5.1.js:869
donejquery-1.5.1.js:6591
jQuery.ajaxTransport.send.callbackjquery-1.5.1.js:7382

在我的函数调用之前有没有办法检查:

$('.upload').fileUploadUI({
 .........
 .........
 .........

由于

3 个答案:

答案 0 :(得分:16)

您可以添加/设置一个类作为排序的标志。在这种情况下,我们将添加一个名为applied

的类
//scroll through each upload item
$('.upload').each(function(){

    //Make sure class is not found
    if (!$(this).hasClass('applied')) 

        //Apply Class and Upload Plugin
        $(this).addClass('applied').fileUploadUI({...}); 
}); 

更新,如yoavmatchulsky所述,你也可以更轻松地做到

$('.upload:not(.applied)').addClass('applied').fileUploadUI({...});

答案 1 :(得分:2)

jQuery File Upload插件将对FileUpload处理程序的引用存储为jQuery data对象。
错误消息“FileUpload with namespace”file_upload“已经分配给此元素”来自插件自己检查此数据引用。

您可以通过以下方式初始化插件以防止出现此错误:

$('.upload').not(function () { return $(this).data('file_upload'); }).fileUploadUI({/*...*/});

答案 2 :(得分:2)

你也可以这样做

if(undefined != $('.upload').data('blueimp-fileupload') ){
    console.log( 'plugin already applied to the element' );
}
else console.log( 'plugin not applied to the element' );
相关问题