您的位置:首页 > Web前端 > JavaScript

Javascript 错误处理:try throw catch

2016-11-02 11:38 771 查看
<body>
<p>请输入1~10的数字:</p>
<input id="input" type="text" onfocus="clearAll(this)">
<input type="button" value="校验" onclick = "check()">
<p id="msg"></p>
</body>


<script>
function check(){
try{
var x = document.getElementById("input").value;
if(x==""){throw "值为空!";}
if(isNaN(x)){throw "不是数字!";}
if(x<1){throw "数字太小!";}
if(x>10){throw "数字太大!";}

}
catch(error){
var y = document.getElementById("msg");
y.innerHTML = "错误:" + error ;
}
}
function clearAll(x){
x.value="";
}
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript