【问题标题】:Customizing jQuery ui datepicker width自定义 jQuery ui datepicker 宽度
【发布时间】:2016-05-19 08:37:19
【问题描述】:

当有人在下面的代码 sn-p 的帮助下单击输入时,我正在使用 jquery ui datepicker 来显示 datepicker。

代码:

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

JSFIDDLE

我遇到的问题是日历应该是它之前输入的全宽。

我确实搜索了互联网并使用了一些代码 sn-ps 但没有任何工作。

我的方法:

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

在 Fiddle 中,输入的宽度固定为 500px,所以 datepicker 宽度也应该是 500px 宽。并且 datepicker 的宽度应该适应这个输入的任何宽度。

【问题讨论】:

标签: jquery jquery-ui jquery-ui-datepicker


【解决方案1】:

您可以使用以下 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">

【讨论】:

  • 但这会使日期选择器的宽度固定。相反,我想要一个 jquery 脚本,使其宽度适应输入的宽度。
  • @KiranKumarDash 输入的宽度将如何变化?您也可以对 datepicker 应用相同的更改...为什么不呢?除非您在 jQuery UI 之上创建插件(您可能不会)我不认为这是一个问题。幕后发生的事情正在阻止/清除脚本设置的内联宽度。
  • 在我的情况下,输入的宽度实际上是以百分比表示的。所以,目前我正在尝试编写一些脚本来首先获取输入的宽度,然后将该宽度分配给 datepicker。
【解决方案2】:

Datepicker 显示函数不幸地重置了宽度参数,甚至在“beforeShow”回调中设置了一个。为了解决这个问题,我们可以在 JS 调用堆栈上显示后通过 setTimeout() 移动它。

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

【讨论】:

  • 不鼓励仅使用代码回答,因为它们没有为未来的读者提供太多信息,请对您所写的内容提供一些解释
猜你喜欢
  • 2013-12-07
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多