从data- *属性获取jquery插件选项?

时间:2013-12-01 16:22:44

标签: javascript jquery html5 jquery-plugins options

是否可以做类似的事情:

<ul class="handle" data-option="9000">

$('.handle').pluginName({
    option: $('this').data('option'),
});

其中$('this')应该定位正在初始化的$('。handle')。

2 个答案:

答案 0 :(得分:3)

不,不是,你可以遍历元素:

$('.handle').each(function() {
   var $this = $(this);
   $this.pluginName({ option: $this.data('option') });
});

如果您是该插件的作者,请阅读插件中的data-*属性。

答案 1 :(得分:1)

“this”周围没有撇号,它应该像这个例子一样工作:

$('.handle').each( function() {
    alert( $(this).data('option') );
} );