简体   繁体   English

自定义jQuery ui datepicker宽度

[英]Customizing jQuery ui datepicker width

I am using jquery ui datepicker to show datepicker when someone clicks on the input with the help of following code snippet. 当有人在以下代码段的帮助下单击输入时,我正在使用jquery ui datepicker显示日期选择器。

Code: 码:

$('#datepicker').datepicker();

JSFIDDLE 的jsfiddle

The problem I have with this is that the calendar should come as full width of the input before it. 我的问题是日历应该以输入之前的完整宽度显示。

I did search the internet and used a few code snippets but none worked. 我确实在互联网上搜索并使用了一些代码段,但没有一个起作用。

My approach: 我的方法:

$('#datepicker').datepicker({
    beforeShow: function(input, inst)
    {
        inst.dpDiv.css({ width: input.width + 'px'});
    }
});

In the Fiddle the input's width is fixed as 500px, so datepicker width should also be 500px wide. 在小提琴中,输入的宽度固定为500px,因此datepicker宽度也应为500px宽。 And datepicker's width should adapt to whatever width is of this input. datepicker的宽度应适应此输入的任何宽度。

You can adjust the calendar size using the below CSS: 您可以使用以下CSS调整日历大小:

 $('#datepicker').datepicker(); 
 #datepicker, .ui-datepicker, .ui-datepicker-header, .ui-datepicker-calendar { width: 500px; } .ui-datepicker-calendar { background: dodgerblue; } .ui-datepicker-header { text-align: center; color: #fff; background: #000; } .ui-datepicker-prev { float: left; } .ui-datepicker-next { float: right; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <input type="text" id="datepicker"> 

Datepicker show function unfortunately resets width parameter, even one set in "beforeShow" callback. Datepicker show函数不幸地重置了width参数,即使是在“ beforeShow”回调中设置了一个也是如此。 To fix this we can move it after show on JS Call Stack by setTimeout(). 为了解决这个问题,我们可以在setTimeout()在JS Call Stack上显示之后将其移动。

$('#datepicker').datepicker({
    beforeShow: function (input, inst) {
        setTimeout(function(){
            inst.dpDiv.outerWidth($(input).outerWidth());
        },0);
    },
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM