简体   繁体   English

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

[英]jQuery, how to check if a plugin has already been applied to a div?

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

I'm using the plugin above. 我正在使用上面的插件。 How in jQuery can I check to see if the fileUpload has already been applied? 如何在jQuery中查看是否已应用fileUpload?

I get the following error now: 我现在收到以下错误:

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

Is there a way to check before my function calls: 有没有一种方法可以在我的函数调用之前进行检查:

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

Thanks 谢谢

You can add/set a class as a flag of sorts. 您可以添加/设置类作为各种标志。 In this case, we'll add a class called applied 在这种情况下,我们将添加一个名为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({...}); 
}); 

Update as pointed out below by yoavmatchulsky , you could also, more easily do yoavmatchulsky在下面指出的更新 ,您也可以更轻松地执行

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

You can also do this 您也可以这样做

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

The jQuery File Upload plugin stores a reference to it's FileUpload handler as jQuery data object. jQuery File Upload插件将对其FileUpload处理程序的引用存储为jQuery数据对象。
The error message "FileUpload with namespace "file_upload" already assigned to this element" comes from the plugin's own check for this data reference. 错误消息“已为该元素分配了名称空间为“ file_upload”的FileUpload”来自插件自己对此数据引用的检查。

You can initialize the plugin the following way to prevent this error: 您可以通过以下方式初始化插件,以防止出现此错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM