<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>捕獲異常</title> </head> <script type="text/javascript"> function my_func(){ try{ x=document.getElementById("input_id").value; alert(x); if(x==""||x==null) throw "非空"; if(isNaN(x)) throw "必須是數字"; if(x<5) throw "數字太小"; if(x>10) throw "數字太大"; else throw "輸入正確"; }catch(err){ document.getElementById("demo").innerHTML="提示:"+err; } } </script> <body> <p>請輸入5——10之間的數字</p> <input type="text" id="input_id"> </input> <button type="button" id="button_id" onclick="my_func()">確定</button> <p id = "demo"></p> </body> </html>