溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

js如何實現(xiàn)多行文本框統(tǒng)計剩余字數(shù)功能

發(fā)布時間:2021-06-21 12:50:46 來源:億速云 閱讀:395 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關(guān)js如何實現(xiàn)多行文本框統(tǒng)計剩余字數(shù)功能的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

效果圖:

js如何實現(xiàn)多行文本框統(tǒng)計剩余字數(shù)功能

代碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>js統(tǒng)計文本框剩余字數(shù)</title>
  <style type="text/css">
    #area{
      width: 300px;
      height: 300px;
      resize:none;
    }
  </style>
</head>
<body>
  <textarea autofocus id="area" onkeydown="sy()" maxlength="10" placeholder="只能輸入十個字"></textarea>
<!--
   resize:none 多行文本框不可以拖動
   onkeypress="sy()"鍵盤按住或點擊時調(diào)用方法
   maxlength="10"定義最大寬度
   placeholder="只能輸入十個字"  定義默認提示
   autofocus  定義自動獲得焦點
   -->
  <span>你還可以輸入:<strong id="span" >10</strong>個字</span>
  <input type="button" value="統(tǒng)計" onclick="fun();">
  <div id="div"></div>
  <script type="text/javascript">
     function sy() {
       var num=document.getElementById("area").value.length;
       //console.log(num);
       var sheng=10-num;
       if(sheng==0){
         //變顏色為紅色
         console.log(sheng);
         document.getElementById("span").style.color="#ff0000";
       }else{
         //變顏色為黑色
         document.getElementById("span").style.color="#000000";
       }
       document.getElementById("span").innerHTML=sheng;
     }
     function fun(){
       var txt=document.getElementById("area").value;
       //這么寫的意思是申請abc三個值都為0
       var a=b=c=0;
       for(var i=0;i<txt.length;i++){
         var ch=txt.charAt(i);
         if(ch>="a"&&ch<="z"){
           a++;
         }else if(ch>="A"&&ch<="Z"){
           b++;
         }else if(ch>="0"&&ch<="9"){
           c++;
         }
       }
       //abc中分別統(tǒng)計了小寫字母、大寫字母、數(shù)字的個數(shù)
       document.getElementById("div").innerHTML="大寫字母有"+b+"個,小寫字母有"+a+"個,數(shù)字有"+c+"個";
     }
  </script>
</body>
</html>

感謝各位的閱讀!關(guān)于“js如何實現(xiàn)多行文本框統(tǒng)計剩余字數(shù)功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI