0

当有人在该文本框中键入任何内容时,我编写了一个代码来在文本字段中显示 a 到 z 但现在在该代码中,当用户按下空格键时,我需要一个一个地显示 a 到 z 并且还更改前一个字母按下空格键时的颜色所以有人可以帮我做到这一点,这是我的代码

<html>
<head>
<script>
    var x=0
    function type()
    {
        var a=newArray("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
       var i=document.getElementById("type").value
       var n=i.substring(0,i.length-1)
       document.getElementById("type").value=n.concat(a[x]);
       if(x>=0)x++
       if(x>=26)x=0 
     }
</script>    
</head>
<body>
<input type="text" name="type" id="type" size="100" onkeyup="type()">
</body>
</html>
4

1 回答 1

0

给你!

var letter_array = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];

var pointer = 0; // start the pointer at 0 which means that the first letter will show is the a
var shown_letters=''
$('#type').keydown(function (e) {
     if (e.keyCode == 32) { 
         if(pointer+1 == 26){
         pointer = 0;
         }
         shown_letters += letter_array[pointer];
         $('#type').val( shown_letters);
         pointer = pointer+1
       return false; // return false to prevent space from being added
     }
});

并且您不能在文本输入中设置与其他字母不同的最后一个字母的样式

http://jsfiddle.net/m4eJx/

于 2013-01-28T07:40:48.227 回答