Rom*_*man 88
我正在使用以下CSS技巧:
input[type="date"]:before {
content: attr(placeholder) !important;
color: #aaa;
margin-right: 0.5em;
}
input[type="date"]:focus:before,
input[type="date"]:valid:before {
content: "";
}
Run Code Online (Sandbox Code Playgroud)
<input type="date" placeholder="Choose a Date" />
Run Code Online (Sandbox Code Playgroud)
Abd*_*ost 19
onfocus="(this.type='date')" onblur="(this.type='text')"
Run Code Online (Sandbox Code Playgroud)
Abd*_*ith 13
您可以在伪之前使用CSS.
.dateclass {
width: 100%;
}
.dateclass.placeholderclass::before {
width: 100%;
content: attr(placeholder);
}
.dateclass.placeholderclass:hover::before {
width: 0%;
content: "";
}
Run Code Online (Sandbox Code Playgroud)
<input
type="date"
placeholder="Please specify a date"
onClick="$(this).removeClass('placeholderclass')"
class="dateclass placeholderclass">
Run Code Online (Sandbox Code Playgroud)
小智 12
你可以这样做:
<input onfocus="(this.type='date')" class="js-form-control" placeholder="Enter Date">
Run Code Online (Sandbox Code Playgroud)
我使用这个 css 方法来模拟输入日期的占位符。
唯一需要js的是设置值的属性,如果使用React,它是开箱即用的。
input[type="date"] {
position: relative;
}
input[type="date"]:before {
content: attr(placeholder);
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #fff;
color: rgba(0, 0, 0, 0.65);
pointer-events: none;
line-height: 1.5;
padding: 0 0.5rem;
}
input[type="date"]:focus:before,
input[type="date"]:not([value=""]):before
{
display: none;
}
Run Code Online (Sandbox Code Playgroud)
<input type="date" placeholder="Choose date" value="" onChange="this.setAttribute('value', this.value)" />
Run Code Online (Sandbox Code Playgroud)