【问题标题】:placeholder for input type date html5输入类型日期html5的占位符
【发布时间】:2015-09-06 19:51:38
【问题描述】:

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

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

相反,它显示 mm/dd/yyy

如何显示日期

【问题讨论】:

标签: html date placeholder


【解决方案1】:

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

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

【讨论】:

  • 这在 safari mobile 中确实有问题
  • 我为 Safari 所做的解决方法是添加 onmouseover="(this.type='date')"
【解决方案2】:

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

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

【讨论】:

  • 几乎可以工作,除了初始加载它仍然有格式。但这已经很接近了。
【解决方案3】:

在这里,我尝试了 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 }
&lt;input type="date" name="dob" data-placeholder="Date of birth" required aria-required="true" /&gt;

希望对你有帮助

【讨论】:

  • 我可以看到它可以在 Chrome 上运行,但不能在 FF 上运行。可能是需要浏览器前缀
【解决方案4】:
<input type="text" placeholder="*To Date" onfocus="(this.type='date')" onblur="(this.type='text')" >

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

【讨论】:

    【解决方案5】:

    对于angular 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">
    

    【讨论】:

      猜你喜欢
      • 2012-03-26
      • 1970-01-01
      • 2013-06-30
      • 2021-02-03
      • 2019-12-13
      • 2017-05-15
      • 2013-12-17
      • 2020-01-30
      相关资源
      最近更新 更多