如何使用jQuery将字符串的首字母转换为大写?

2021年3月19日13:56:17 发表评论 1,001 次浏览

任务是大写字符串的第一个字母而不使用toUpperCase()方法借助jQuery。下面讨论了两种方法:

方法1:在此示例中, css()方法用于设置文字转换属性重视大写.

例子:

<!DOCTYPE HTML> 
< html > 
  
< head > 
     < title > 
         How to convert first letter of a
         string to upper case using jQuery?
     </ title >
      
     < script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js" >
     </ script >
</ head > 
  
< body style = "text-align:center;" > 
      
     < h1 style = "color:green;" > 
         lsbin 
     </ h1 >
      
     < p >
         Click on the button to
         perform the operation.
     </ p >
      
     Type Here: < input id = "input" />
     < br >< br >
      
     < button onclick = "GFG_Fun()" >
         Click Here
     </ button >
      
     < p id = "GFG" ></ p >
      
     < script >
         var geeks = document.getElementById('GFG');
          
         function GFG_Fun() {
             $('#input').css('textTransform', 'capitalize');
             geeks.innerHTML = "Text is capitalized";
         }
     </ script > 
</ body > 
  
</ html >

输出如下:

如何使用jQuery将字符串的首字母转换为大写?1

方法二:在此示例中, 我们使用CSS属性执行操作。新的ID已添加到设置属性的元素中文字转换to大写.

例子:

<!DOCTYPE HTML> 
< html > 
  
< head > 
     < title > 
         How to convert first letter of a
         string to upper case using jQuery?
     </ title >
      
     < script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js" >
     </ script >
      
     < style >
         #capital {
             text-transform: capitalize;
         }
     </ style >
</ head > 
  
< body style = "text-align:center;" > 
      
     < h1 style = "color:green;" > 
         lsbin 
     </ h1 >
      
     < p >
         Click on the button to
         perform the operation.
     </ p >
      
     Type Here: < input id = "input" />
     < br >< br >
      
     < button onclick = "GFG_Fun()" >
         Click Here
     </ button >
      
     < p id = "GFG" ></ p >
      
     < script >
         var geeks = document.getElementById('GFG');
          
         function GFG_Fun() {
             $('#input').attr('id', 'capital');
             geeks.innerHTML = "Text is capitalized";
         }
     </ script > 
</ body > 
  
</ html >

输出如下:

如何使用jQuery将字符串的首字母转换为大写?2

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: