且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果我在样式中设置默认大写,如何在同一文本框中键入大写和小写字母的混合?

更新时间:2022-04-27 23:00:58

答案代码如下,

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Change caps to small</title>
<script type="text/javascript">
var bx, chr, indx, intvl, last, lng, ln2, str, tmp;
function PrepBox()
{
    bx = document.getElementById("txtbx");
    last = "";     //this will be the manipulated string
    lng = 0;       //its length
    intvl = setInterval("txtchk();", 100); //ten times per second
    return;
}
function txtchk()
{ 
tmp = bx.value;
ln2 = tmp.length;

if(ln2 < lng) //test for backspace
    last = tmp.substr(0,ln2);  //shorten the manipulated string

else if(ln2 > lng)
{ 
    str = tmp.substr(lng);   //get newly-typed character(s)

    for(indx=0; indx<str.length; indx++)  //for each one of them
    { 
        chr = str.charAt(indx);  
        if((chr >= "a") && (chr <= "z"))
            last += chr.toUpperCase();   //change lowercase to upper
        else  if((chr >= "A") && (chr <= "Z"))
            last += chr.toLowerCase();   //change uppercase to lower
        else
            last += chr;   //periods, commas, semicolons, ...
    }
}
lng = ln2;  //update saved-length of "last"
bx.value = last; //replace text in box with manipulated string
return;
}
</script>
</head>
<body>
<input id="txtbx" size="50" maxlength="40" type="text" style="font-size:10pt;" onFocus="PrepBox();" />
</body>
</html>

输出:

1。如果大写锁定已关闭,则默认输入文本为大写,如果我按Shift键输入文本,则会更改为小。

2。如果打开大写锁定,默认输入文本很小,如果我按Shift键输入文本,则更改为大写。