placeholder for input type date html5

前端 未结 5 962
臣服心动
臣服心动 2020-12-19 10:38

placeholder does not work for input type date directly.


相关标签:
5条回答
  • 2020-12-19 10:56

    use onfocus="(this.type='date')", example:

    <input required="" type="text" class="form-control" placeholder="Date" onfocus="(this.type='date')"/>
    
    0 讨论(0)
  • 2020-12-19 10:57

    use onfocus and onblur... Here it's an example:

    <input type="text" placeholder="Birth Date" onfocus="(this.type='date')" onblur="if(this.value==''){this.type='text'}">
    
    0 讨论(0)
  • 2020-12-19 11:03

    For angular 2,You can use this directive

    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) { }
    
    }
    

    and use it like below.

     <input appDateClick name="targetDate" placeholder="buton name" type="text">
    
    0 讨论(0)
  • 2020-12-19 11:05

    Here, I have tried data attribute in input element. and applied required placeholder using 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" />

    Hope this helps

    0 讨论(0)
  • 2020-12-19 11:19
    <input type="text" placeholder="*To Date" onfocus="(this.type='date')" onblur="(this.type='text')" >
    

    This code works for me. Simply you can use this

    0 讨论(0)
提交回复
热议问题