编写函数实现单击按钮,判断用户输入的字符是不是数字,如果不是,则显示不是数字;如果是整数,将其转换成浮点数,并显示到网页上;如果是浮点数,将其转换成整数,并且显示到网页上。
<!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>
<script>
function dispatchClick() {
const value = prompt("请输入一个值")
const isNaN = window.isNaN(Number(value))
if (!isNaN) {
const numberTypeValue = Number(value)
const ret = Math.ceil(numberTypeValue) === numberTypeValue ? numberTypeValue.toFixed(2) : parseInt(numberTypeValue)
alert(ret)
} else {
alert("不是数字")
}
}
</script>
</head>
<body>
<div onclick="dispatchClick()">按钮</div>
</body>
</html>