添加回调的正确方法

添加回调的正确方法

本文介绍了向 jQuery DatePicker 添加回调的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DatePicker 有一个内部函数,_adjustDate(),我想在它之后添加一个回调.

DatePicker has an internal function, _adjustDate(), which I'd like to add a callback after.

这就是我目前的做法.基本上,只是用它自己替换函数定义,加上新的操作.这会影响所有日期选择器,我不确定是否需要.

This is how I'm currently doing this. Basically, just replacing the function defintion with itself, plus the new operations. This effects all datepickers, which I'm not sure is desired, either.

$(function(){

   var old = $.datepicker._adjustdate;

   $.datepicker._adjustDate = function(){
      old.apply(this, arguments);

      // custom callback here
      console.log('callback');
   };

});

_adjustDate 是在下个月/上个月点击期间调用的日期.该函数接受三个参数.我很好奇是否有更好的方法来添加回调.

_adjustDate is what's called during the next/prev month clicks. The function takes three parameters. I'm curious if there's a better way to add the callback.

我希望最终结果是这样的;其中 afterAjustDate 是回调句柄:

I'd like to have the end result look like this; where afterAjustDate is the callback handle:

$('#datepicker').datepicker({
       showButtonPanel: true
     , afterAdjustDate: function(){ $(this).find('foo').addClass('bar'); }
});


仅供参考


onChangeMonthYear 不是可接受的事件替代方案,live()/delegate() 也不是(不适用于 IE)绑定选项.


FYI


onChangeMonthYear is not an acceptable event alternative, neither are the live()/delegate() (don't work in IE) binding options.

这来自于选择月份后应用类/操作元素的需要.更改月份会重新创建日历中的元素;执行此操作时,jQueryUI 不够智能,无法继承类更改.因此,我需要在更改日期后回调.

This comes from the need of applying classes/manipulating elements after the month is selected. Changing the month recreates elements in the calendar; when this is performed jQueryUI is not smart enough to inherit the class changes. Thus, I need a callback after changing the date.

推荐答案

我写了一个小演示来实现这个...

I wrote a small demo that does this...

我创建了一个包含 $.datepicker 扩展名的对象文字,然后在 $.datepicker 和我的对象上执行 $.extend.

I create an object literal that contains the extensions to $.datepicker and then do $.extend on $.datepicker and my object.

您可以在这里查看:http://jsfiddle.net/NHN4g/4/

这是扩展本身:

(function($){
    var datepickerExtensions = {
        _oldAdjustDate: $.datepicker._adjustDate,
        _adjustDate: function(id, offset, period) {
            var target = $(id);
            var inst = this._getInst(target[0]);
            var afterAdjustDate = this._get(inst, 'afterAdjustDate');
            this._oldAdjustDate(id, offset, period);
            if(afterAdjustDate && typeof afterAdjustDate === 'function'){
                afterAdjustDate(id, offset, period);
            }
        }
    }
    $.extend($.datepicker, datepickerExtensions);
})(jQuery);

和演示:

(html)

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css" media="all">
<div class="demo">
    <p>Date: <input type="text" id="datepicker"></p>
</div><!-- End demo -->

(javascript)

(javascript)

var changed = false;
$("#datepicker").datepicker({
    afterAdjustDate: function(i,o,p){
        if(!changed){
            $('.ui-datepicker-month').css('color', '#f00');
        }
        changed = !changed;
    }
});

这篇关于向 jQuery DatePicker 添加回调的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:34
查看更多