简体   繁体   English

获取jQuery UI Datepicker的高度和宽度

[英]Get the Height and Width of a jQuery UI Datepicker

I'm using the jQuery UI Datepicker widget. 我正在使用jQuery UI Datepicker小部件。 When the user changes the month or year on the widget my code makes an ajax request to the server to get the highlighted dates for that month/year. 当用户更改窗口小部件上的月份或年份时,我的代码向服务器发出ajax请求,以获取该月/年的突出显示日期。 In order to give the user an indication that the webpage is waiting for the server I'm trying to grey out the datepicker and display a loading image. 为了向用户提供网页正在等待服务器的指示,我正试图使日期选择器变灰并显示加载图像。 I can already do all of this, except, I'm having problems getting the height and width of the greyed out section to match the height and width of the datepicker widget. 我已经可以完成所有这些了,除了我在获取灰色部分的高度和宽度以匹配datepicker小部件的高度和宽度方面遇到了问题。

Because the datepicker's height can change based on the number of weeks in a month each time I called the overlay I was trying to use jQuery to get the height of the widget and then display the overlay. 因为每次调用叠加层时,日期选择器的高度可以根据一个月内的周数来改变,所以我试图使用jQuery来获取窗口小部件的高度,然后显示叠加层。 However, the height never seems to match the actual height of the datepicker. 但是,高度似乎永远不会与日期选择器的实际高度相匹配。 Here are the two ways I've tried setting the height. 以下是我尝试设置高度的两种方法。

$('#loading').height($('.ui-datepicker-calendar').height() + $('.ui-datepicker-header').height())

and

$('#loading').height($('.ui-datepicker-inline.ui-datepicker.ui-widget.ui-widget-content.ui-helper-clearfix.ui-corner-all').height())

Neither of which work. 两者都不奏效。 Does anyone know a way I can programmatically get the height and width of a jQuery UI Datepicker widget? 有没有人知道我可以通过编程方式获取jQuery UI Datepicker小部件的高度和宽度?

This is a long shot as I can't tell exactly what you mean by the height never seems to match the actual height , other than it's not covering the whole datepicker. 这是一个很长的镜头,因为我无法准确地说出你的意思, the height never seems to match the actual height ,除了它没有覆盖整个日期选择器。 A solution to this may be outerWidth() and outerHeight() . 对此的解决方案可能是outerWidth()outerHeight() These functions get the width of the element including border, padding and (optionally) margins: 这些函数获取元素的宽度, 包括边框,填充和(可选)边距:

$('#loading').height($('.ui-datepicker-calendar').outerHeight(false) + $('.ui-datepicker-header').outerHeight(false))

$('#loading').height($('.ui-datepicker-inline.ui-datepicker.ui-widget.ui-widget-content.ui-helper-clearfix.ui-corner-all').outerHeight(false))

outerHeight(false) means "get the height plus padding and border, but leave out the margin". outerHeight(false)表示“获取高度加上填充和边框,但省略边距”。 Set it to true if you want to include the margin too. 如果要包含边距,请将其设置为true

height() works fine, the problem was that someone else had edited the file and added padding without my knowledge. height()工作正常,问题是其他人编辑了文件并在我不知情的情况下添加了填充。 So when I set the height it always had a bunch of extra pixels added, making my overlay too large for the datepicker widget. 因此,当我设置高度时,它总是添加了一堆额外的像素,使得我的叠加层对于datepicker小部件来说太大了。 In addition it is sufficient to select .ui-datepicker-inline . 另外,选择.ui-datepicker-inline就足够了。 So my final jQuery code is: 所以我最后的jQuery代码是:

$('#loading').height($('.ui-datepicker-inline').height());

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

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