2013年11月19日 星期二

不要將任何事情都在jQuery.ready()初始化

一般會用「datapicker」這個jQuery UI widget來選擇日期
大部分的人會預設ready就載入

$( document ).ready( function() {

    $( "input.date" ).datepicker({
        minDate: moment().subtract( "months", 1 ).toDate(),
        maxDate: moment().add( "months", 1 ).toDate(),
        dateFormat: "d M, y",
        constrainInput: true,
        beforeShowDay: $.datepicker.noWeekends
    });

});
 
 
當USer input時才載入.效能會比較好 
 
$( document ).on( "focus", "input.date:not(.hasDatepicker)", function() {
    toastr.info( "Initializing " + this.id );

    $( this ).datepicker({
        minDate: moment().subtract( "months", 1 ).toDate(),
        maxDate: moment().add( "months", 1 ).toDate(),
        dateFormat: "d M, y",
        constrainInput: true,
        beforeShowDay: $.datepicker.noWeekends
    });
}); 

沒有留言:

張貼留言