How to add placeholder <input type=“datetime-lo

2019-08-11 09:15发布

问题:

I've been trying to add placeholder in input type='datetime-local' field but it doesn't work at all. Use css for solving the issue but still unable to do it :(

			input[type="datetime-local"]:before{
    content: 'Selecteer Datum';
    color: #000;
    text-align: center;
    width:100%;
}
input[type="datetime-local"]:active:before, input[type="datetime-local"]:hover:before, input[type="datetime-local"]:visited:before,  input[type="datetime-local"]:focus:before{
    content: '';
    width: 100%;
}
<form action="" method="post">
    <div class="datetime">
        <input type="datetime-local"  >
    </div>
</form>
    
    

回答1:

This is kind of a wonky idea, change the input type to text, then it will convert to datetime-local on focus using the below javascript.

<input type="text" id="txtbox" placeholder="Heya">

<script>
  var dtt = document.getElementById('txtbox')
  dtt.onfocus = function (event) {
      this.type = 'datetime-local';
      this.focus();
  }
</script>


回答2:

Sharath Daniel is on the right lines. A jQuery version: http://jsfiddle.net/2e97L3d0/1/

HTML

<form action="" method="post">
<div class="datetime">
    <input type="text" id="datetime1" placeholder="Enter the starting date"  >
</div>

Javascript

$(document).ready(function(){
$("#datetime1").focus( function() {
    $(this).attr({type: 'datetime-local'});
  });

});



回答3:

A bit tricky, but it works:

Html:

<form action="" method="post">
    <div class="datetime">
        <input id="pholderInput" class="placeholder"  type="datetime-local">
    </div>
</form>

Css:

.placeholder
{
  color: gray;
}

.focused
{
  color: black;
}

Javascript:

var inp = document.getElementById("pholderInput");

var placeholderText = "default text";

inp.value = placeholderText;

inp.onfocus = function(e) {
    if (inp.value == placeholderText)
    {
        inp.value = "";
        inp.className = "focused";
    }
};

inp.onblur = function(e) {
    if (inp.value === "")
    {
        inp.value = placeholderText;
        inp.className = "placeholder";
    }
};

JSBin



回答4:

Try this code:

$(document).ready(function(){
    $("input[type='datetime-local']").attr('placeholder','date time');
});

FIDDLE DEMO