js错误try...catch...

<p>Please input a number between 5 and 10:</p>
<input id="demo2" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="mess"></p>

 

<script type="text/javascript">
function myFunction(){
var x = document.getElementById("demo2").value;
var y = document.getElementById("mess");
try{
if(x=="") throw "empty";
if(x>10) throw "too high";
if(x<5) throw "too low";
if(5 <= x <= 10) throw "is just";
if(isNaN(x)) throw "not a number";
}catch(err){
y.innerHTML = "Error:"+err+".";
}
}

posted on 2017-05-12 16:12  IT_包子  阅读(113)  评论(0)    收藏  举报

导航