How to add placeholder <input type=“datetime-local”> field?

后端 未结 5 678
忘了有多久
忘了有多久 2021-01-21 06:36

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 :(

5条回答
  •  终归单人心
    2021-01-21 07:05

    A bit tricky, but it works:

    Html:

    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

提交回复
热议问题