Jquery-ui 数据采集器更改 z-index

这个问题很简单,尽管我很难找到解决它的方法。

我使用了一个 jQuery-ui 数据采集器和一个定制的“ ios 样式打开/关闭切换”。这个切换使用一些绝对定位的元素,它们当前显示在我的日期选择器的顶部。

看看下面7月6日那个丑陋的圆圈。

enter image description here

这样做的肮脏方法(至少是 imo)是在我的样式表中编写一个样式,但是当选择器启动时,我更愿意使用一些 javascript 来完成这项工作。

我已经试过了

$('.date_field').datepicker();
$('.date_field').datepicker("widget").css({"z-index":100});

还有

$('.date_field').datepicker({
beforeShow: function(input, inst) {
inst.dpDiv.css({"z-index":100});
}
});

但似乎每次启动数据采集器时,z 索引都会被覆盖。

感谢你的帮助!

108551 次浏览

Your JS code in the question doesn't work because jQuery resets the style attribute of the datepicker widget every time you call it.

An easy way to override its style's z-index is with a !important CSS rule as already mentioned in another answer. Yet another answer suggests setting position: relative; and z-index on the input element itself which will be automatically copied over to the Datepicker widget.

But, as requested, if for whatever reason you really need to set it dynamically, adding more unnecessary code and processing to your page, you can try this:

$('.date_field').datepicker({
//comment the beforeShow handler if you want to see the ugly overlay
beforeShow: function() {
setTimeout(function(){
$('.ui-datepicker').css('z-index', 99999999999999);
}, 0);
}
});

Fiddle

​I created a deferred function object to set the z-index of the widget, after it gets reset'ed by the jQuery UI, every time you call it. It should suffice your needs.

The CSS hack is far less ugly IMO, I reserve a space in my CSS only for jQuery UI tweaks (that's right above the IE6 tweaks in my pages).

There is a more elegant way to do it. Add this CSS:

.date_field {position: relative; z-index:100;}

jQuery will set the calendar's z-index to 101 (one more than the corresponding element). The position field must be absolute, relative or fixed. jQuery searches for the first element's parent, which is absolute/relative/fixed, and takes its' z-index

The datepicker now sets the popup z-index to one more than its associated field, to keep it in front of that field, even if that field is itself in a popup dialog. By default the z-index is 0, so datepicker ends up with 1. Is there a case where this is not showing the datepicker properly? JQuery Forum Post

To get a z-index of 100. You need an input with z-index:99; and JQueryUI will update the datepicker to be z-index:100

<input style="z-index:99;"> or <input class="high-z-index"> and css .high-z-index { z-index: 99; }

You can also specify the z-index to inherit which should inherit from your dialog box and cause jQueryUI to properly detect the z-index.

<input style="z-index:inherit;"> or <input class="inhert-z-index"> and css .inherit-z-index { z-index: inherit; }

You need to use !important clause to force the in-line z-index value using CSS.

.ui-datepicker{z-index: 99 !important};

This worked for me when I was trying to use datepicker in conjunction with a bootstrap modal:

$('input[id^="txtDate"]').datepicker();
$('input[id^="txtDate"]').on('focus', function (e) {
e.preventDefault();
$(this).datepicker('show');
$(this).datepicker('widget').css('z-index', 1051);
});

Of course, change the selector to fit your own need. I set the "z-index" to 1051 because the z-index for the bootstrap modal was set to 1050.

The BEST NATURAL way is to simply put the date-picker element on a "platform" that has a "relative" position and has a higher "z-index" than the element that is showing up above your control...

In my case nothing worked. I needed to add the z-index to the input type that has the datepicker.

<input type="text" class="datepicker form-control" datatype="date" style="z-index: 10000;" id="txtModalDate">

This is for Bootstrap datetimepicker

If your datepicker is hiding because of scroll appears in your div use:

overflow-x: visible !important;
overflow-y: visible !important;

in css of whole div that contain your datepicker and other item such as

.dialogModel{
overflow-x: visible !important;
overflow-y: visible !important;
}

Add this:

    $(document).ready(function()
{
$('#datepickers-container').css('z-index', 999999999);
};