How to wrap the datepicker in a new div?

∥☆過路亽.° 提交于 2019-12-01 23:02:34

问题


I need to put my datepicker in a new div, that will be a (shadow-)border-div.

I've tried the following:

beforeShow: function (input) {
  $(input).datepicker("widget")
   .find(".ui-datepicker-header, ui-datepicker-calendar")
   .wrapAll("<div class='datepickerBorder'/>");
}

But it does not work.

Additionally I've tried to wrap the whole picker, but then the border-div has not the same position, size, etc.


回答1:


The datepicker control is absolutely positioned. Wrapping it inside an element will not contain the datepicker inside that element -- the wrapping element will just sit at the bottom of the page while the datepicker will render next to the control.

Solution 1: You can add the class to the datepicker widget itself:

$("#datepicker2").datepicker({
    beforeShow: function() {
        $(this).datepicker("widget").addClass("datepickerBorder");
    },
    onClose: function() {
        $(this).datepicker("widget").removeClass("datepickerBorder");
    }
});

demo

Solution 2: create an absolutely positioned + hidden wrapping div on DOM load and re-position + re-size the div behind the datepicker when it shows. There is a catch: you cannot check the widget's offset (the co-ordinates at which it will render) inside the beforeShow function.




回答2:


$('#datepicker').wrap('<div class="datepickerBorder"/>');

Here #datepicker is the id of datepicker input field, you may have something else.

Check jQuery .wrap().

You don't need to do this within beforeShow(). Do it within DOM ready function.

To wrap each inner element of datepicker you should try

$('div#ui-datepicker-div > *').wrap('<div class='datepickerBorder'/>');


来源:https://stackoverflow.com/questions/10812921/how-to-wrap-the-datepicker-in-a-new-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!