如何编写我的第一个jQuery插件? [英] How to write my first jQuery Plugin?
问题描述
我现在正在编写我的第一个jQuery插件,我发现了有关,但是在第一步中,他们提供了两种编写插件的方法: 第一个:
I'm now writing my first jQuery Plugin and i found this blog post about 'Building you first jQuery plugin' but in the first step they give 2 ways of writing the plugin: The First One:
$.fn.yourPlugin = function(options) {
return this.each(function() {
});
};
第二个:
(function($){
$.fn.yourPlugin = function() {
return this.each(function() {
});
};
})(jQuery);
因为他们说$
可能存在一些问题,例如与另一个库的冲突....
Because They say there could be some problems with the $
like conflicts with another libraries....
所以我想知道什么是最佳实践.
So I'd like to know what's the best practice here.
推荐答案
最佳实践肯定是第二种选择. jQuery有关于插件创作的出色文章,其中还包含许多其他最佳实践.还有一个开始的模板",使您的第二个选择更进一步.
Best practice is definitely the second option. jQuery has a great article on plugin authoring with a number of additional best practices. There's also a starting "template" that takes your second option a bit further.
这篇关于如何编写我的第一个jQuery插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!