test.html
capitalize.js
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />//解决乱码
<!DOCTYPE html>
<html>
<head>
<script src="capitalize.js"type="text/javascript"></script>
</head>
<body>
<h1>名字大小写转换器(单数位大写,双数位小写)</h1>
<p>请输入您的英文名字:</p>
<div>
<input id="name"type="text"size="24"/>
<button onclick="cap();">开始大小写转换!</button>
</div>
</body>
</html>
function cap(){
var input=document.getElementById("name");
var name=input.value;
var newName="";
for(var i=0;i<name.length;i++){
if(i%2==0)
newName+=name.charAt(i).toUpperCase();
else
newName+=name.charAt(i).toLowerCase();
}
input.value=newName;
}