Jquery 插件编写:如何在代码中使用选项?
所以我正在转换一个插件。最初我有一个传统的 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);
}
);
}
});
};
我如何将该颜色选项值传递给其下面的悬停函数?或者更简单地说,我可以将选项值传递给变量吗?
So I'm converting a plugin. Originally I had a traditional javascript function with some variables I pass in, but I wanted to make it a full Jquery plugin with options. The documentation shows you how to set them up, but not how to use them in your code. Here's a bit of the code:
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);
}
);
}
});
};
How would I pass that color option value to the hover function beneath it? Or more simply, can I just pass the option value to a variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已声明一个名为
settings
的变量。您可以使用settings.hoverColor
访问该对象的hoverColor
属性:插件的
options
参数也将是一个对象。当您传入该值时,extend
方法将合并options
对象和“defaults”对象的内容,并将合并结果分配给设置
。You have declared a variable called
settings
. You can access thehoverColor
property of that object withsettings.hoverColor
:The
options
argument to your plugin will be an object too. When you pass that in, theextend
method will merge the contents of theoptions
object and the "defaults" object, and you assign the result of that merge tosettings
.以下应该有效:
或者 jcreamer898 所说的也有效:
The following should work:
Or what jcreamer898 said works as well:
这应该有效...
This should work...