第一个字母大写

时间:2013-04-12 10:57:59

标签: javascript

在ASP.NET中使用JavaScript,我需要将小写字母转换为大写字母。

如何使用JavaScript中的正确答案解决此问题?

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server">
<title></title>

<script language="JAVASCRIPT">
     function capitalizeMe(obj) {
         val = obj.value;
            newVal = '';
         val = val.split(' ');
         for (var c = 0; c < val.length; c++) {
             newVal += val[c].substring(0, 1).toUpperCase() +
             val[c].substring(1, val[c].length) + ' ';
         }   
     }
</script>

</head>
<body>
     <form id="Form2" runat="server">
        <asp:TextBox ID="TextBox1" runat="server" onblur="capitalizeMe(this)">
        </asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
     </form> 
  </body>
</html>

2 个答案:

答案 0 :(得分:3)

为什么不使用css?

.capitalized{
    text-transform: capitalize
}

将类添加到元素中并让css处理其余的

答案 1 :(得分:0)

 <script language="JAVASCRIPT">
 function capitalizeMe(obj) {
    val = obj.value;
        newVal = '';
     val = val.split(' ');
     for (var c = 0; c < val.length; c++) {
    newVal += val[c].substring(0, 1).toUpperCase() +
 val[c].substring(1, val[c].length) + ' ';
  }
    obj.value = newVal;
   }
    </script>
相关问题