输入类型日期html5的占位符

时间:2015-06-21 05:57:24

标签: html5 date placeholder

占位符不能直接用于输入类型日期。

<input required="" type="text" class="form-control" placeholder="Date"/>

相反,它显示 mm / dd / yyy

如何显示日期

5 个答案:

答案 0 :(得分:23)

使用onfocus="(this.type='date')",例如:

<input required="" type="text" class="form-control" placeholder="Date" onfocus="(this.type='date')"/>

答案 1 :(得分:8)

使用onfocus和onblur ......这是一个例子:

<input type="text" placeholder="Birth Date" onfocus="(this.type='date')" onblur="if(this.value==''){this.type='text'}">

答案 2 :(得分:2)

<input type="text" placeholder="*To Date" onfocus="(this.type='date')" onblur="(this.type='text')" >

此代码对我有用。只需您可以使用此

答案 3 :(得分:1)

对于角度2,您可以使用此指令

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">

答案 4 :(得分:1)

在这里,我在data元素中尝试了input属性。并使用CSS

应用所需的占位符
<input type="date" name="dob" data-placeholder="Date of birth" required aria-required="true" />

&#13;
&#13;
    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 }
&#13;
<input type="date" name="dob" data-placeholder="Date of birth" required aria-required="true" />
&#13;
&#13;
&#13;

希望这有帮助

相关问题