admin管理员组文章数量:1421751
How does one make a jQuery plugin that is possible to execute without using an element selector?
Like, usually it's:
$('#myElementID').myPluginFunction(myVariables);
But, instead:
$.myPluginFunction(myVariables);
Thanks in advance!
How does one make a jQuery plugin that is possible to execute without using an element selector?
Like, usually it's:
$('#myElementID').myPluginFunction(myVariables);
But, instead:
$.myPluginFunction(myVariables);
Thanks in advance!
Share Improve this question asked Feb 24, 2012 at 14:39 tomsseisumstomsseisums 13.4k20 gold badges88 silver badges148 bronze badges4 Answers
Reset to default 6Actually you just use $.extend()
instead of $.fn.extend()
, read here for more
$.fn.extend({
myMethod: function(){...}
});
//jQuery("div").myMethod();
$.extend({
myMethod2: function(){...}
});
//jQuery.myMethod();
You can do it like this:
$.myPluginFunction = function(vars) {
//...
};
Use $.extend
. Here's a small example:
$(function() {
return $.extend({
addDebugBorder: function(selection, size) {
return selection.each(function() {
var color, randomColor;
randomColor = function() {
return Math.floor(Math.random() * 256);
};
if (!size) size = '5px';
color = 'rgb(' + randomColor() + ',' + randomColor() + ', ' + randomColor() + ')';
return $(this).css('border', size + ' solid ' + color);
});
}
});
});
Then called like
$.addDebugBorder($("table tr td"), '1px');
edit- This example code should really have been a normal plugin, I think I modified it to take the selector rather then just working on div's only is why its a little weird.
Instead of assigning the plugin to the jQuery.fn
object, just assign it to the root jQuery object itself.
jQuery.myPluginFunction = function(myVariables) {
// plugin code
}
本文标签: javascriptjQuery plugin to work without elementStack Overflow
版权声明:本文标题:javascript - jQuery plugin to work without element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745329736a2653756.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论