【发布时间】:2011-12-13 05:10:55
【问题描述】:
我有一个 jQuery 插件,它遵循 docs.jQuery.com 的插件/创作示例,但我无法弄清楚如何为应用插件的每个元素保留唯一设置。页面上有两个元素,每个元素都分别应用了插件。
到目前为止,这是我的代码:
(function( $ ) {
/* Default settings */
var defaults = {
id: null,
properties: null,
container: '.comp-settings', /* override for .retail or .distressed */
priceLabel: '.comp-section-header span',
slide: slideCallback,
minRange: 100000
};
/* Some public here: $.compSlider('method', options) */
var methods = {
init: function( options ) {
var settings = $.extend( {}, defaults, options );
return this.each(function() {
$(this).slider( settings );
});
}
}
/* Some methods here */
function opt( name ) {
return settings[ name ];
}
/* Plugin namespace */
$.fn.compSlider = function( method ) {
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else if ( typeof method === 'string' ) {
return this.slider( method, Array.prototype.slice.call( arguments, 1 ) );
}
};
})( jQuery )
问题:
- 无法在插件函数中访问设置变量
- 如果我将设置变量存储在 $(this).data('settings') 中,我仍然无法从 'opt' 函数访问 $(this).data('settings'),因为“this”不是t 元素(我相信它是 DOM 窗口。)
感谢任何帮助。
【问题讨论】:
标签: jquery plugins jquery-plugins scope