Javascript 如何在jQuery datepicker中指定多个选项
我正在使用。我使用下面的脚本将datepicker附加到类为datepicker的所有控件上Javascript 如何在jQuery datepicker中指定多个选项,javascript,jquery,datepicker,Javascript,Jquery,Datepicker,我正在使用。我使用下面的脚本将datepicker附加到类为datepicker的所有控件上 <script type="text/javascript"> $(document).ready(function(){ $(".datepicker").datepicker({ showButtonPanel: true}); }); </script> 它似乎不起作用。你需要写: $(".datepicker").datepicker({ showB
<script type="text/javascript">
$(document).ready(function(){
$(".datepicker").datepicker({ showButtonPanel: true});
});
</script>
它似乎不起作用。你需要写:
$(".datepicker").datepicker({ showButtonPanel: true, showOtherMonths: true });
这是jQuery中的一个常见习惯用法,使用JSON对象(哈希)作为选项。大多数jQuery插件的参数之一接受对象作为参数,例如
{option1:'optionValue'}
。这允许用户传入他们想要从默认值更改的任何选项。在大多数插件中,使用$.extend()
将此选项对象与默认选项合并,如果定义了选项对象,则使用选项对象的属性覆盖任何默认选项
编写插件的一种常用方法如下
(function($) {
$.fn.myPlugin = function(options) {
var settings = $.extend({ // these are the default properties
option1: 'default value',
option2: true,
option3: 5437
},
// merge with the options object passed in
// or an empty object
options || {});
// now we can use the settings object in our plugin, which will have
// property values from the settings object and the options object passed in
}
})(jQuery);
$.extend()
中的options | |{}
部分防止为选项传递错误值。$.extend()
中对象的顺序决定了哪些属性优先-在对象上定义的属性将替换其值(此处大致使用术语)在后面的对象上声明相同的属性名。这是什么:hash vs options
optionsobject
或optionsobject hash
是描述同一事物的两个术语。散列部分源于我所知的散列表
,其中每个属性都有一个键(其名称)和一个值。如果有帮助的话,可以将对象哈希
想象成JavaScript版本的关联数组。这只是标准的json,我认为将其称为哈希是非常误导的。区别在于datepicker接受一个对象。一个对象应该包含所有要设置的选项。上述问题为每个选项提供了一个对象。
(function($) {
$.fn.myPlugin = function(options) {
var settings = $.extend({ // these are the default properties
option1: 'default value',
option2: true,
option3: 5437
},
// merge with the options object passed in
// or an empty object
options || {});
// now we can use the settings object in our plugin, which will have
// property values from the settings object and the options object passed in
}
})(jQuery);