在以下程序中,出于某种原因,for循环只运行一次,然后不再重复。我相信错误是用粗体代码。非常感谢帮助。这是一个用于将文本框更改为大写字母,标题大小写等的程序。标题大小写是每个大写字母的首字母大写。谢谢。For Loop仅运行一次
<html>
<head>
<script type="text/javascript">
function titlize(){
tLength=tBox.box.value.length
character=new Array()
for(i=1; i<tLength+1; i++){
**character[i]=tBox.box.value.slice(i-1,i)**
document.write(character[i])
if(i==1){
character[i]=character[i].toUpperCase()
}else if(character[i-1]==" "){
character[i]=character[i].toUpperCase()
}else{
character[i]=character[i].toLowerCase()
}
document.write(i)
document.write(character[i])
}
}
function upperC(){
toUpperCase(tBox.box.value)
}
function verify(){
if(tBox.uppercase.checked){
tBox.box.value=tBox.box.value.toUpperCase()
}
if(tBox.lowercase.checked){
tBox.box.value=tBox.box.value.toLowerCase()
}
if(tBox.titlecase.checked){
titlize()
}
if(tBox.uppercase.checked){
tBox.box.value=tBox.box.value.toUpperCase()
}
}
</script>
</head>
<body>
<form name="tBox">
<input type="text" name="box" value=""><br>
<input type="checkbox" name="uppercase" onClick=verify(this.form)>Uppercase<br>
<input type="checkbox" name="lowercase" onClick=verify(this.form)>Lowercase<br>
<input type="checkbox" name="titlecase" onClick=verify(this.form)>Titlecase<br>
</form>
</body>
</html>
参见:http://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript 功能toTitleCase(STR){ 返回str.replace (/ \ w \ S */g,function(txt){return txt.charAt(0).toUpperCase()+ txt.substr(1).toLowerCase();}); } –