<html>
<head>
<title>js实现首字母大写</title>
<script language="
javascript">
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; //显示转换后的文本
}
</script></head>
<body>
<input type=text id="txt1"name="txt1" value="this is test!">
<input type=button value="转换文本"
onclick="javascript:changeCase(txt1)"></body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>首字母大写</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script language="JavaScript">
function update(s) {
s.value = s.value.replace(/^(.)/mg, function(x) {return x.toUpperCase()});
}
</script>
</head>
<body>
<form name="form1" id="form1" method="post" action="">
<textarea name="txt"></textarea>
<input type="button" name="btn1" value="Convert" onClick="update(this.form.txt)" />
</form>
</body>
</html>