显示输入类型日期的占位符文本
Show placeholder text for input type date
占位符不适用于直接输入类型 date
和 datetime-local
。
<input type="date" placeholder="Date" />
<input type="datetime-local" placeholder="Date" />
该字段在桌面设备上显示 mm/dd/yyy,在移动设备上不显示任何内容。
如何显示 日期 占位符文本?
使用onfocus="(this.type='date')"
,示例:
<input required="" type="text" class="form-control" placeholder="Date" onfocus="(this.type='date')"/>
使用onfocus和onblur...这是一个例子:
<input type="text" placeholder="Birth Date" onfocus="(this.type='date')" onblur="if(this.value==''){this.type='text'}">
对于angular2,可以使用这个指令
import {Directive, ElementRef, HostListener} from '@angular/core';
@Directive({
selector: '[appDateClick]'
})
export class DateClickDirective {
@HostListener('focus') onMouseFocus() {
this.el.nativeElement.type = 'date';
setTimeout(()=>{this.el.nativeElement.click()},2);
}
@HostListener('blur') onMouseBlur() {
if(this.el.nativeElement.value == ""){
this.el.nativeElement.type = 'text';
}
}
constructor(private el:ElementRef) { }
}
并像下面这样使用它。
<input appDateClick name="targetDate" placeholder="buton name" type="text">
在这里,我尝试了 input
元素中的 data
属性。并使用 CSS
应用所需的占位符
<input type="date" name="dob" data-placeholder="Date of birth" required aria-required="true" />
input[type="date"]::before {
content: attr(data-placeholder);
width: 100%;
}
/* hide our custom/fake placeholder text when in focus to show the default
* 'mm/dd/yyyy' value and when valid to show the users' date of birth value.
*/
input[type="date"]:focus::before,
input[type="date"]:valid::before { display: none }
<input type="date" name="dob" data-placeholder="Date of birth" required aria-required="true" />
希望对您有所帮助
<input type="text" placeholder="*To Date" onfocus="(this.type='date')" onblur="(this.type='text')" >
这段代码对我有用。你可以简单地使用这个
现代浏览器使用 Shadow DOM 来简化日期和日期时间的输入。因此,除非浏览器出于任何原因选择回退到 text
输入,否则不会显示占位符文本。您可以使用以下逻辑适应这两种情况:
::-webkit-calendar-picker-indicator {
@apply hidden; /* hide native picker icon */
}
input[type^='date']:not(:placeholder-shown) {
@apply before:content-[attr(placeholder)];
@apply sm:after:content-[attr(placeholder)] sm:before:hidden;
}
input[type^='date']::after,
input[type^='date']::before {
@apply text-gray-500;
}
我使用了 Tailwind CSS 语法,因为它很容易理解。让我们一块一块地分解它:
::-webkit-calendar-picker-indicator {
@apply hidden; /* hide native picker icon */
}
使用阴影 DOM pseudo-element 选择器隐藏本机选择器图标(通常是日历)。
input[type^='date']:not(:placeholder-shown) {
@apply before:content-[attr(placeholder)];
@apply sm:after:content-[attr(placeholder)] sm:before:hidden;
}
Select 未显示占位符的所有 date
和 datetime-local
输入以及:
- 默认使用
::before
pseudo-element 显示输入的 placeholder
文本
- 在小屏幕及更高版本的屏幕上切换为使用
::after
pseudo-element
input[type^='date']::after,
input[type^='date']::before {
@apply text-gray-500;
}
设置 ::before
和 ::after
pseudo-element 的样式。
占位符不适用于直接输入类型 date
和 datetime-local
。
<input type="date" placeholder="Date" />
<input type="datetime-local" placeholder="Date" />
该字段在桌面设备上显示 mm/dd/yyy,在移动设备上不显示任何内容。
如何显示 日期 占位符文本?
使用onfocus="(this.type='date')"
,示例:
<input required="" type="text" class="form-control" placeholder="Date" onfocus="(this.type='date')"/>
使用onfocus和onblur...这是一个例子:
<input type="text" placeholder="Birth Date" onfocus="(this.type='date')" onblur="if(this.value==''){this.type='text'}">
对于angular2,可以使用这个指令
import {Directive, ElementRef, HostListener} from '@angular/core';
@Directive({
selector: '[appDateClick]'
})
export class DateClickDirective {
@HostListener('focus') onMouseFocus() {
this.el.nativeElement.type = 'date';
setTimeout(()=>{this.el.nativeElement.click()},2);
}
@HostListener('blur') onMouseBlur() {
if(this.el.nativeElement.value == ""){
this.el.nativeElement.type = 'text';
}
}
constructor(private el:ElementRef) { }
}
并像下面这样使用它。
<input appDateClick name="targetDate" placeholder="buton name" type="text">
在这里,我尝试了 input
元素中的 data
属性。并使用 CSS
<input type="date" name="dob" data-placeholder="Date of birth" required aria-required="true" />
input[type="date"]::before {
content: attr(data-placeholder);
width: 100%;
}
/* hide our custom/fake placeholder text when in focus to show the default
* 'mm/dd/yyyy' value and when valid to show the users' date of birth value.
*/
input[type="date"]:focus::before,
input[type="date"]:valid::before { display: none }
<input type="date" name="dob" data-placeholder="Date of birth" required aria-required="true" />
希望对您有所帮助
<input type="text" placeholder="*To Date" onfocus="(this.type='date')" onblur="(this.type='text')" >
这段代码对我有用。你可以简单地使用这个
现代浏览器使用 Shadow DOM 来简化日期和日期时间的输入。因此,除非浏览器出于任何原因选择回退到 text
输入,否则不会显示占位符文本。您可以使用以下逻辑适应这两种情况:
::-webkit-calendar-picker-indicator {
@apply hidden; /* hide native picker icon */
}
input[type^='date']:not(:placeholder-shown) {
@apply before:content-[attr(placeholder)];
@apply sm:after:content-[attr(placeholder)] sm:before:hidden;
}
input[type^='date']::after,
input[type^='date']::before {
@apply text-gray-500;
}
我使用了 Tailwind CSS 语法,因为它很容易理解。让我们一块一块地分解它:
::-webkit-calendar-picker-indicator {
@apply hidden; /* hide native picker icon */
}
使用阴影 DOM pseudo-element 选择器隐藏本机选择器图标(通常是日历)。
input[type^='date']:not(:placeholder-shown) {
@apply before:content-[attr(placeholder)];
@apply sm:after:content-[attr(placeholder)] sm:before:hidden;
}
Select 未显示占位符的所有 date
和 datetime-local
输入以及:
- 默认使用
::before
pseudo-element 显示输入的placeholder
文本 - 在小屏幕及更高版本的屏幕上切换为使用
::after
pseudo-element
input[type^='date']::after,
input[type^='date']::before {
@apply text-gray-500;
}
设置 ::before
和 ::after
pseudo-element 的样式。