<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" id="txt">
<button id="btn">按钮</button>
<script>
btn.onclick=function(){
// 如果可以转化数值类型,直接就转化了。否则得到的是NaN
// NaN和自身也不相等
var m=parseInt(txt.value);
console.log(m);
if(isNaN(m)){
alert('请输入数值类型');
}
}
</script>
</body>
</html>
isNaN(参数) 函数用于参数是否是非数字值。
如果参数为 NaN 或字符串、对象、undefined等非数字值则返回 true, 否则返回 false。