jQuery插件模板

jQuery plugin template

本文关键字:插件 jQuery      更新时间:2023-09-26

我找到了这个创建jQuery插件的模板http://stefangabos.ro/jquery/jquery-plugin-boilerplate-revisited/

但我不明白在代码中,为什么我们像jQuery对象的方法一样声明pluginName,然后像对象$.fn的方法一样宣布pluginName?我们为什么这么做?

将插件添加到$可以编写:

var $el = $(".myselector");
$.myplugin($el, {
  some: "option",
  another: "one"
});

将插件添加到$.fn使您能够编写:

$(".myselector").myplugin({
  some: "option",
  another: "one"
});