<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <input id="demo" type="text"> <script> function myFunction(){ var x = document.getElementById("demo").value; if(x == "" || isNaN(x)){ console.info(x+" :不是数字"); }else{ console.info(x+" :是数字"); } } </script> <button type="button" onclick="myFunction()">点击这里</button> </body> </html>
效果图: