Chrome 以外的浏览器的 input[type='date'] CSS 样式

Hui*_*Tan 5 css firefox date input microsoft-edge

是否有等效的CSS代码

::-webkit-datetime-edit
::-webkit-datetime-edit-fields-wrapper
::-webkit-datetime-edit-text
::-webkit-datetime-edit-month-field
::-webkit-datetime-edit-day-field
::-webkit-datetime-edit-year-field
::-webkit-inner-spin-button
::-webkit-calendar-picker-indicator
Run Code Online (Sandbox Code Playgroud)

适用于 Firefox 和 Microsoft Edge?到目前为止,我找不到任何有关 的占位符样式的文档/资源<input type='date'>。如有任何建议/答案,我们将不胜感激。

尝试了::placeholder,::-ms-input-placeholder但当::-moz-placeholder输入类型为日期时它们不起作用。

或者,如果有人可以告诉我如何隐藏默认占位符,我很乐意接受答案。

Zhi*_* Lv 1

通过使用F12开发者工具检查HTML和CSS,我们可以看到Chrome浏览器使用了user agent sytelsheet,这些伪元素(::-webkit)适用于chrome浏览器,但在Microsoft Edge浏览器中,它没有使用user agent sytelsheet ,并且这些伪元素不会应用于输入日期文本框。因此,该代码无法在 Microsoft Edge 中运行。

所以,我认为你可以尝试使用 Microsoft Edge Dev 版本(基于 Chromium),代码在上面运行良好。

否则,作为解决方法,我建议您可以参考以下代码来使用文本框和日期选择器插件来显示日期。

<style>
    .input-field {
        position: relative;
        display: inline-block;
    }

        .input-field > label {
            position: absolute;
            left: 0.5em;
            top: 50%;
            margin-top: -0.5em;
            opacity: 0.5;
        }

        .input-field > input[type=text]:focus + label {
            display: none;
        }

        .input-field > label > span {
            letter-spacing: -2px;
        }

    .month {
        color: cornflowerblue;
    }

    .day {
        color: aqua;
    }

    .year {
        color:darkmagenta
    }

    .separate-letter {
        color: red
    }
</style>
<link href="http://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<div class="input-field">
    <input id="input-text-field" type="text" class="date" data-selecteddate="" value="" />
    <label for="input-text-field">
        <span id="span_month" class="month">MM</span>
        <span class="separate-letter">/</span>
        <span  id="span_day" class="day">DD</span>
        <span class="separate-letter">/</span>
        <span id="span_year"  class="year">YYYY</span>
    </label>
</div>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script>
    $(function ($) {
        $(".date").datepicker({
            onSelect: function (dateText) {
                //display("Selected date: " + dateText + "; input's current value: " + this.value);
                var dataarray = dateText.split("/")
                $("#span_month").html(dataarray[0]);
                $("#span_day").html(dataarray[1]);
                $("#span_year").html(dataarray[2]);

                //clear the textbox value
                this.value = "";
                $("#input-text-field").attr("data-selecteddate", dateText);
            }
        })            
    });
</script>
Run Code Online (Sandbox Code Playgroud)

结果如下(使用 Microsoft Edge 浏览器):

在此输入图像描述