English first letter all uppercase JS realizes script _javascript skill

Source: Internet
Author: User
Tags strlen
Complete code:
<script language= "JavaScript" > <!--Begin function ChangeCase (frmobj) {var index; var tmpstr; var Tmpchar; var prestring; var poststring; var strlen; Tmpstr = FrmObj.value.toLowerCase (); StrLen = Tmpstr.length; if (StrLen > 0) {for (index = 0; index < strLen; index++) {if (index = = 0) {Tmpchar = tmpstr.substring (0,1). To Uppercase (); poststring = tmpstr.substring (1,strlen); TMPSTR = Tmpchar + poststring; else {Tmpchar = tmpstr.substring (index, index+1); if (Tmpchar = = "" && Index < (strLen-1)) {Tmpchar = tmp Str.substring (index+1, index+2). toUpperCase (); prestring = tmpstr.substring (0, index+1); poststring = tmpstr.substring (Index+2,strlen); TMPSTR = prestring + Tmpchar + poststring; }}} Frmobj.value = Tmpstr; }//End--> </script> <form name=form> <input type=text name=box value= ' type in here! ' > <input type=button value= "Convert" onclick= "Javascript:changecase (this.form.box)" > </form>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Script Description:
The first step: Add the following code to the <script language= "JavaScript" >
<!--Begin
function ChangeCase (frmobj) {
var index;
var tmpstr;
var Tmpchar;
var prestring;
var poststring;
var strlen;
Tmpstr = FrmObj.value.toLowerCase ();
StrLen = Tmpstr.length;
if (StrLen > 0) {
for (index = 0; index < strLen; index++) {
if (index = = 0) {
Tmpchar = tmpstr.substring (0,1). toUpperCase ();
poststring = tmpstr.substring (1,strlen);
TMPSTR = Tmpchar + poststring;
}
else {
Tmpchar = tmpstr.substring (index, index+1);
if (Tmpchar = = "" && Index < (strLen-1)) {
Tmpchar = tmpstr.substring (index+1, index+2). toUpperCase ();
prestring = tmpstr.substring (0, index+1);
poststring = tmpstr.substring (Index+2,strlen);
TMPSTR = prestring + Tmpchar + poststring;
}
}
}
}
Frmobj.value = TMPSTR;
}
End-->
</script>


Step Two: Add the following code to the <body> area
<form name=form>
<input type=text name=box value= "type in here!" >
<input Type=button value= "Convert" onclick= "Javascript:changecase (this.form.box)" >
</form>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.