JQuery 插件默认值和选项
如何将插件中的以下属性/变量转换为可以从文档中设置的默认值和选项?
// 插件 js:
(function($){
$.fn.myPlugin = function(options){
var myForm = this;
myForm.variable1 = true;
myForm.variable2 = true;
myForm.variable3 = true;
myForm.variable4 = true;
...
if(myForm.variable1){
// do something
}
...
}
})(jQuery);
// 页面中准备好的文档:
<script type="text/javascript">
$(document).ready(function() {
$('#form1').myPlugin();
});
</script>
How can I turn the following properties / variables in my plugin into defaults and options that can be set from document ready?
// plugin js:
(function($){
$.fn.myPlugin = function(options){
var myForm = this;
myForm.variable1 = true;
myForm.variable2 = true;
myForm.variable3 = true;
myForm.variable4 = true;
...
if(myForm.variable1){
// do something
}
...
}
})(jQuery);
// document ready in page:
<script type="text/javascript">
$(document).ready(function() {
$('#form1').myPlugin();
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的模式是扩展默认选项对象。但这确实意味着任何参数都必须作为“选项”对象一起传递,例如:
myPlugin({variable2:false})
The simplest pattern is to extend a default options object. But it does mean that any parameters have to be passed together as an "option" object, eg:
myPlugin({variable2:false})
请参阅以下内容:
并使用
有关正确方法,请参阅
http://jquery-howto.blogspot.com/2009/01/how-to-set-default-settings-in-your.html
See following:
and use
For proper way see
http://jquery-howto.blogspot.com/2009/01/how-to-set-default-settings-in-your.html