jQuery UI Datepicker - How to alter Datepicker HTML

主宰稳场 提交于 2019-12-03 23:21:42

It seems like you need to wait till the .ui-datepicker-calendar table to be inserted into the #ui-datepicker-div to append your message. You could do a timer to check for that:

$('#datepicker').datepicker({
    beforeShow: function(input, inst) {
        insertMessage();
    }
});

// listen to the Prev and Next buttons
$('#ui-datepicker-div').delegate('.ui-datepicker-prev, .ui-datepicker-next', 'click', insertMessage);

function insertMessage() {
    clearTimeout(insertMessage.timer);

    if ($('#ui-datepicker-div .ui-datepicker-calendar').is(':visible'))
        $('#ui-datepicker-div').append('<div>foo</div>');
    else
        insertMessage.timer = setTimeout(insertMessage, 10);
}

See it in action: http://jsfiddle.net/william/M9Z7T/2/.

See it in action: http://jsfiddle.net/M9Z7T/126/.

   $('.datepicker').datepicker({ beforeShow: function () {
        setTimeout(appendsomething, 10);
    },
    onChangeMonthYear: function () {
        setTimeout(appendsomething, 10);
        }
    }
    );

var appendsomething = function () {
    $("#ui-datepicker-div").append("<div class='something'>something</div>");
}

I use the following trick to monkey-patch the _generateHTML function. The following example replaces some jQuery-ui classes with jQuery-mobile classes using regular expressions. Modify according to your needs:

(function () {
    $.datepicker._generateHTML_old = $.datepicker._generateHTML;
    $.datepicker._generateHTML = function (inst) {
        var html = this._generateHTML_old(inst);
        html = html.replace(/<a class="(ui-datepicker-prev ui-corner-all)( ui-state-disabled)?"/, '<a class="$1 ui-btn ui-btn-left ui-icon-carat-l ui-btn-icon-notext$2"');
        html = html.replace(/<a class="(ui-datepicker-next ui-corner-all)( ui-state-disabled)?"/, '<a class="$1 ui-btn ui-btn-right ui-icon-carat-r ui-btn-icon-notext$2"');
        html = html.replace(/<span class="ui-icon ui-icon-circle-triangle-.">(.+?)<\/span>/g, '$1');
        return html;
    };
})();

Edit: using regex/string functions to edit HTML is actually a bad idea. Consider placing the HTML inside an element, manipulate, and grab the resulting HTML.

It can be done as simply as this (this works with the new datepicker):

$('.datepicker').datepicker('show');
$(".ui-datepicker").append(<div>Foo</div>);

$(document).on('click', '.ui-datepicker-next', function () {
    $(".ui-datepicker").append(<div>Foo</div>);
})

$(document).on('click', '.ui-datepicker-prev', function () {
    $(".ui-datepicker").append(<div>Foo</div>);
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!