How to set placeholder value using CSS?

后端 未结 9 1294
天涯浪人
天涯浪人 2020-12-16 09:00

I want to set the placeholder value of an input box using only css and not JavaScript or jQuery. How can I do this?

9条回答
  •  再見小時候
    2020-12-16 09:27

    I recently had to do this with google's search box, this is an extreme hack reserved for extreme situations (the resulting selector was slightly different, but I made it work in this example)

    /*
     this is just used to calculate the resulting svg data url and need not be included in the final page
    */
    
    var text = placeholder.outerHTML;
     var url = "data:image/svg+xml;,"+text.replace(/id="placeholder"/g," ").replace(/\n|([ ] )/g,"");//.replace(/" /g,"\"");
    img.src = url;
    result.value = url;
    overlay.style.backgroundImage = "url('"+url+"')";
    svg,img{
      border: 3px dashed black;
    }
    textarea{
    width:50%;
    height:300px;
    vertical-align: top;
    }
    .wrapper{
      position: relative;
      display: inline-block;
    }
    #overlay{
      position:absolute;
      left:0;
      top:0;
      right:0;
      bottom:0;
      pointer-events: none;
      background-repeat: no-repeat;
      background-position: center left;
    }
    #my_input:focus + #overlay{
      display: none;
    }
    As SVG Some New Rad Placeholder
    
    As IMG
    As Data URI
    As "Placeholder"

提交回复
热议问题