如何更改jQuery UI Datepicker的位置? [英] How to change jquery ui datepicker position?
问题描述
是否可以更改 jQuery UI datepicker 的top
和left
位置(获取当前值并更改它们).请注意,我需要更改位置,而不像在其他示例中那样设置margin
.
Is it possible to change top
and left
positions (get current values and change them) of jQuery UI datepicker. Please note that i need to change position, not set margin
as it is in other examples.
推荐答案
可以.由于始终只有一个日期选择器处于活动状态,因此可以使用以下方式选择活动的日期选择器:
Sure it is. As there's always only one datepicker active, you can select active datepicker with:
var $datepicker = $('#ui-datepicker-div');
并更改其位置:
$datepicker.css({
top: 10,
left: 10
});
编辑
哇,棘手的一个.如果在beforeShow
中设置顶部或左侧位置,则datepicker插件会再次覆盖该位置.您必须将CSS更改放入setTimeout
:
Whoah, tricky one. If you set top or left position in beforeShow
, it gets overriden again by datepicker plugin. You have to put css changes in a setTimeout
:
$("#datepicker").datepicker({
beforeShow: function (input, inst) {
setTimeout(function () {
inst.dpDiv.css({
top: 100,
left: 200
});
}, 0);
}
});
演示: http://jsfiddle.net/BWfwf/4/
关于setTimeout(function () {}, 0)
的解释:为什么有时setTimeout(fn,0)有用?
这篇关于如何更改jQuery UI Datepicker的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!