How can I give different color for each letter in a text field?

前端 未结 3 1030
余生分开走
余生分开走 2020-12-06 18:59

I have an input text field like this,



        
3条回答
  •  温柔的废话
    2020-12-06 19:14

    Why not make the input's font invisible and have some javascript that dynamically changes some text placed over the input as you type? Something like this:

    Name:

    JavaScript:

    $(document).ready(function(){
        $('.text').keyup(function(){
            var output="";
            var letters = $(this).val().split("");
            letters.forEach(function(letter){
                var color = "#"+(Math.random()*16777215|0).toString(16);
                //Stolen from Derek's answer ;)
                output += '' + letter + '';
              $('div.colors').html(output);
            });
        });
    });
    

    Then you just gotta position the div over the input; et voila! Not tested.. but I am making a jsFiddle now! http://jsfiddle.net/pranavcbalan/54EY4/6/

提交回复
热议问题