<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script> window.onload = function(){ var oText = document.getElementById("text1"); oText.onkeydown = function(ev){ var oEvent = ev ||event; //不是数字 并且 不是退格 // alert(oEvent.keyCode); //oEvent.keyCode = 8 s是退格 if((oEvent.keyCode <48 ||oEvent.keyCode>57) && oEvent.keyCode!=8) { return false; } } } </script> </head> <body> <input type="text" id="text1" /> </body> </html>