所以我要转换一个插件。最初,我具有传递一些变量的传统javascript函数,但我想使其成为带有选项的完整Jquery插件。该文档显示了如何设置它们,但没有显示如何在代码中使用它们。下面是一些代码:
jQuery.fn.placeholderRX = function(options) {
var settings = jQuery.extend({
hoverColor: '#fff',
addClass: 'null',
textColor: '#FBFBFB'
}, options);
var $this = $(this);
$this.children('.inputSpan').each(function() {
$(this).children('.inputText').hover(
function() {
$input.children('.input').css('background-color', hoverColor);
},
function() {
$input.children('.input').css('background-color', revertColor);
}
);
}
});
};
如何将颜色选项值传递给它下面的悬停功能?或者更简单地说,我可以仅将选项值传递给变量吗?
最佳答案
您已经声明了一个名为settings
的变量。您可以使用hoverColor
访问该对象的settings.hoverColor
属性:
$input.children('.input').css('background-color', settings.hoverColor);
插件的
options
参数也将是一个对象。当您传入它时,extend
方法将合并options
对象和“ defaults”对象的内容,然后将合并结果分配给settings
。关于javascript - jQuery插件编写:如何在代码中使用选项?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8158627/