监听keypress事件。判断如果是数字的话阻止浏览器冒泡

 

<input type="text" id="test">
    <script type="text/javascript">
    window.onload=function(){
             document.getElementById('test').addEventListener('keypress',function(e){
             var charCode=e.charCode;
             if(charCode>46&&charCode<58)  /*0-9 的charcode*/
             e.preventDefault();
     });
    }
    </script>

Demo

相关文章:

  • 2021-07-08
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
猜你喜欢
  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
相关资源
相似解决方案