获取对jQuery UI Widget所有实例的引用? [英] Get reference to all instances of jquery ui widget?
问题描述
我正在编写一个jQuery UI小部件,它仅包装了bootstrap popover插件,在该小部件中,您可以传递选项"singular", 如果传入此参数,则应调用该插件的所有其他实例的函数.
I am writing a jquery UI widget that simply wraps the bootstrap popover plugin, In the widget you can pass in the option 'singular', if this is passed in then it should call a function of all other instances of the plugin.
类似
$('#one').myWidget();
$('#two').myWidget();
$('#three').myWidget();
$('#four').myWidget();
$('#one').myWidget('show'); //stuff from widget one is now visible
$('#two').myWidget('show'); //stuff from widget one and two are now visible
$('#three').myWidget('show'); //stuff from widget one, two and three are now visible
$('#two').myWidget('hide'); //stuff from widget one and three are now visible
$('#four').myWidget('show', {singular:true}); //stuff from widget four is now visible
所以,我想象显示函数看起来像:
So, I imagine the show function looking like:
show: function(options){
options = options || {};
if(options.singular){
var instances = '????'; // how do I get all instances?
$.each(instances, function(i, o){
o.myWidget('hide');
});
}
this.element.popover('show');
}
那么,问题是,我如何获得所有在其上具有myWidget
小部件的元素的引用?
So, question being, how would I get a reference to all elements that have the myWidget
widget on them?
推荐答案
您可以使用$(':ui-myWidget')
,其中ui
是窗口小部件的名称空间.它比使用诸如$('.ui-myWidget')
之类的类选择器要慢,因此在创建窗口小部件时添加类仍然是一种好习惯.
You can use $(':ui-myWidget')
where ui
is your widget's namespace. It is slower than using a class selector like $('.ui-myWidget')
so it is still good practice to add the class when your widget gets created.
jQuery UI对所有小部件都执行此操作,因此您可以通过$(':ui-progressbar')
或$('.ui-progressbar')
来获取每个进度条.
jQuery UI does this for all there widgets so you could get each progressbar by either $(':ui-progressbar')
or $('.ui-progressbar')
.
此博客文章深入解释了
这篇关于获取对jQuery UI Widget所有实例的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!