關於js的keyCode

來源:互聯網
上載者:User

標籤:class   code   c   http   com   string   

原生js的event對象有三個鍵盤事件的值:
1) charCode: 被點擊鍵的Unicode值
2) keyCode: 被點擊鍵的ASCII十進位值
3) which: 字母數字鍵的charCode或者keyCode,具體可以自己試試

Mootools的event被封裝後,統一成為code和key:

?
12 event.code = event.which || event.keyCode;event.key = String.fromCharCode(code).toLowerCase();

寫了個方法,用於控制數字輸入

?
1234567891011121314151617181920 // 價格字串格式(小數點後最多兩位)$$(‘.jsForPrice‘).addEvents({     ‘keypress‘: function(ev){          var result;          if(this.val().length === 0 || this.val().test(‘\\.‘)){// 只能有1個點號               result = ev.code>=48&&ev.code<=57 || ev.code==8;          }else{               result = ev.code>=48&&ev.code<=57||ev.code==8||ev.code==46;          }          return result;     },     ‘keyup‘: function(ev){          if(this.val().test(‘\\.‘)){               if(this.val().split(‘.‘)[1].length>2){                    this.val(this.val().substring(0, this.val().length-1))               }          }          //console.log(this.val().test(‘^[0-9]+(\\.[0-9]{1,2}){0,1}$‘))     }});

參考:stackoverflow

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.