javascript 使用 jQuery 获取 Dropzone 实例/对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29517885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Dropzone Instance/Object using jQuery
提问by preston
I use jQuery to work with dropzone. e.g.
我使用 jQuery 来处理 dropzone。例如
$("#mydropzone").dropzone({ /*options*/ });
I need to get the Dropzone instance so I can call this method:
我需要获取 Dropzone 实例,以便我可以调用此方法:
myDropzone.processQueue()
How can I achieve this? Is it possible?
我怎样才能做到这一点?是否可以?
in other words, how can I initialise drop zone using
换句话说,我如何使用
$("#mydropzone").dropzone({ url: "/file/post" });
but at the same time get the instance of the object as if i initialise it using:
但同时获取对象的实例,就像我使用以下方法初始化它一样:
var myDropzone = new Dropzone("#mydropzone", { url: "/file/post"});
so I may call:
所以我可以打电话:
myDropzone.processQueue()
Thank you very much.
非常感谢你。
采纳答案by Jordan
The script seems to add a dropzone
object to the given element
.
So you can do something like this :
该脚本似乎向dropzone
给定的element
. 所以你可以做这样的事情:
var $dropZone = $("#mydropzone").dropzone({ /*options*/ });
// ...
$dropZone[0].dropzone.processQueue();
回答by cweston
As described in issue #180
You can also use the built in Dropzone.forElement
function.
您也可以使用内置Dropzone.forElement
函数。
var myDropzone = Dropzone.forElement("#mydropzone");
回答by Fabian von Ellerts
Easy way to access the Instance with jQuery, if it has already been initialized:
使用 jQuery 访问实例的简单方法,如果它已经被初始化:
var dropzone = $(this).get(0).dropzone;
回答by Luca Reghellin
As stated before, you can use dropzone's forElement
, which in turn just checks for element.dropzone
, where 'element' is the native one (not jquery obj). So, to merge, summarize, explain and expand the previous answers, you can do like this:
如前所述,您可以使用 dropzone 的forElement
,它反过来只检查element.dropzone
,其中“元素”是本机元素(不是 jquery obj)。因此,要合并、总结、解释和扩展以前的答案,您可以这样做:
var element = $("#mydropzone")[0]; // this is the way jquery gives you the original dom
or better, in just plain js:
或者更好,在简单的 js 中:
var element = document.querySelector("#mydropzone");
then get the dropzone like this:
然后像这样得到 dropzone:
element.dropzone
Or, more concise (plain js here):
或者,更简洁(这里是纯 js):
var dzone = document.querySelector("#mydropzone").dropzone
Just for reference, the current dropzone's forElement
source:
仅供参考,当前 dropzone 的forElement
来源:
Dropzone.forElement = function (element) {
if (typeof element === "string") {
element = document.querySelector(element);
}
if ((element != null ? element.dropzone : undefined) == null) {
throw new Error("No Dropzone found for given element. This is probably because you're trying to access it before Dropzone had the time to initialize. Use the `init` option to setup any additional observers on your Dropzone.");
}
return element.dropzone;
};
回答by Vuong Passion
it's seem simple with Dropzone.instances check with id of element:
使用 Dropzone.instances 检查元素 ID 似乎很简单:
function fn_check_has_dropzone_instances(id){
var found = false;
Dropzone.instances.forEach(function(item,index){
if($($(item)[0].element).attr('id').trim()==id.trim()){
console.log(id);
found = true;
}
});
return found;
}