【发布时间】:2014-08-31 19:25:40
【问题描述】:
我是编写 jQuery 插件的新手,但我对使用它们有一定的经验。我使用过的一些插件只能以编程方式初始化,即
$("input[name='blah']").fancyPants();
我觉得这很烦人,但我已经设法从教程和查看其他插件的代码中弄清楚如何编写这样的基本插件。
其他的,比如Bootstrap的modal组件,可以纯粹通过markup来初始化,即
<input type='text' name='blah' class='fancy-pants'>
然后只需包含插件 .js 文件,任何带有 fancy-pants 类的输入都会自动初始化为 fancyPants 插件。我喜欢这种行为,即使我知道它占用了一个特定的类名。
问题 1:他们是如何做到的?我浏览了bootstrap.js,但里面似乎有很多我不明白的地方。
问题 2: 假设我有一个包含 DOM 中多个元素的插件。例如,
<button class="bootstrapradio" name="primary_group" value="epicurist"><i class='fa fa-cutlery'></i></button>
<button class="bootstrapradio" name="primary_group" value="futurist"><i class='fa fa-space-shuttle'></i></button>
<button class="bootstrapradio" name="primary_group" value="stoic"><i class='fa fa-tree'></i></button>
<button class="bootstrapradio" name="beverage" value="beer"><i class='fa fa-beer'></i></button>
<button class="bootstrapradio" name="beverage" value="martini"><i class='fa fa-glass'></i></button>
在不改变标记结构(例如添加包装容器)的情况下,我怎样才能自动初始化我的插件的两个实例,一个覆盖所有按钮的类为bootstrapradio,名称为@987654329 @,另一个覆盖了类为bootstrapradio、名称为beverage的所有按钮?
编辑:这是我现在正在使用的内容。
(function ( $ ) {
var methods = {
init : function(options) {
// Initialization method, adds classes to elements and binds events
...
return this;
},
value : function( new_val ) {
// Method to get/set value
...
}
};
$.fn.bootstrapradio = function(methodOrOptions) {
if ( methods[methodOrOptions] ) {
return methods[ methodOrOptions ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) {
// Default to "init"
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + methodOrOptions + ' does not exist on jQuery.bootstrapradio' );
}
};
}( jQuery ));
我希望这个插件如何转换 DOM 的示例:
在document.ready之前:
<button class="bootstrapradio" name="primary_group" value="epicurist" title='Epicurist' disabled><i class='fa fa-cutlery'></i></button>
<button class="bootstrapradio" name="primary_group" value="futurist" title='Futurist'><i class='fa fa-space-shuttle'></i></button>
<button class="bootstrapradio" name="primary_group" value="stoic" title='Stoic'><i class='fa fa-tree'></i></button>
<button class="bootstrapradio" name="beverage" value="beer" title='Beer'><i class='fa fa-beer'></i></button>
<button class="bootstrapradio" name="beverage" value="martini" title='Martini'><i class='fa fa-glass'></i></button>
document.ready之后:
<button class="btn btn-xs bootstrapradio" name="primary_group" value="epicurist" title='Epicurist' disabled><i class='fa fa-cutlery'></i></button>
<button class="btn btn-xs bootstrapradio" name="primary_group" value="futurist" title='Futurist'><i class='fa fa-space-shuttle'></i></button>
<button class="btn btn-xs bootstrapradio" name="primary_group" value="stoic" title='Stoic'><i class='fa fa-tree'></i></button>
<input type="hidden" name="primary_group">
<button class="btn btn-xs bootstrapradio" name="beverage" value="beer" title='Beer'><i class='fa fa-beer'></i></button>
<button class="btn btn-xs bootstrapradio" name="beverage" value="martini" title='Martini'><i class='fa fa-glass'></i></button>
<input type="hidden" name="beverage">
【问题讨论】:
-
初始化到底是什么意思?
-
我认为 OP 想知道如何利用 say
data-role=modal和类似的属性来触发特定的 JS 事件。 @ArtOfCode -
是的,我明白了,我只是不知道他想在这个初始化中做什么。
-
是的,我希望插件在具有特定属性(例如
class或data-*)的元素上自动调用其init方法。 -
如果你要走这条路,你还应该考虑设置一些默认值,这样用户就可以通过传入他们自己的选项来覆盖这些默认值。这也是很常见的模式:stackoverflow.com/questions/8905507/…
标签: javascript jquery html twitter-bootstrap jquery-plugins