<body>
<html>
<script>
function f() {
try {
//獲取用戶輸入的數據
var x = document.getElementById("demo").value;
if (x == "") throw "值爲空";
if(isNaN(x)) throw "不是數字";
if(x>=10 ) throw "太大";
if(x<=5) throw "太小";
}
//e 接受throw 的錯誤信息
catch (e) {
var y=document.getElementById("ee");
y.innerHTML = "錯誤" + e
}
}
</script>
<p>請輸入一個數字</p>
<input id = "demo" type="text">
<button type = button onclick="f()">測試輸入</button>
<p id="ee"></p>
</body>
</html>