注:个人练习编写的代码。
利用parseInt()和parseFloat()方法进行判断:
<!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>测试代码页</title>
<style>
#number{
color: black;
font-size: 16px;
}
</style>
</head>
<body>
请输入数字:<input type="text" id="Num"/>
<button onclick="newNum()">按钮</button>
<hr>
<div id="number"></div>
<script>
function newNum() {
var inputNum = document.getElementById("Num"); //①定位:定位到input对象
var Num = inputNum.value; //②操作:然后把值取出来
console.log(Num);
//判断条件
var n = Num;
if (parseInt(Num.toString()) == parseFloat(Num.toString())) {
n = Num + "是整数。";
} else {
n = Num + "是小数。";
}
//返回结果
var divNum = document.getElementById("number"); //①定位:定位到div对象
document.getElementById("number").innerHTML = n; //②操作:把值写进去
}
</script>
</body>
</html>
结果: