2017年8月15日 星期二

javascript 計算字數,不含空白


HTML:
    <input id="word1" size="4" style="color: #FF0000; background-color:yellow; border-   style: solid; border-color: #99CC00" value="0" readonly type="text">
<textarea id=c1 cols=80 rows=10 name=c1>


JAVASCCRIPT

      <script>
       document.getElementById('c1').onkeyup = function() { countword(this.value,'word1');};
       document.getElementById('c2').onkeyup = function() { countword(this.value,'word2');};
       document.getElementById('c3').onkeyup = function() { countword(this.value,'word3');};
       document.getElementById('c4').onkeyup = function() { countword(this.value,'word4');};
      
       function countword(obj,to) {
          t = obj.toString();
          t = t.replace(/\s/g,'');
          document.getElementById(to).value = t.length;
       }
      
       </script>

沒有留言:

張貼留言