JavaScript try-catch-throw

来源:互联网 发布:广西南宁智尚网络骗局 编辑:程序博客网 时间:2024/05/22 17:04

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script>
function myFunction(){
try
{
var x=document.getElementById("demo").value;
if (x == "") throw "值为空";
if(isNaN(x)) throw "输入值不是数字";
if(x<6) throw "值太小" ;
if(x>10) throw "值太大";
}
catch(err)
{
var y =document.getElementById("mess");
y.innerHTML = "错误:" + err + ".";
}
}
</script>
</head>
<body>
<h1>我的测试程序</h1>
<p>请输入6-10之间的数字</p>
<input id="demo"type="text"/>
<button type="button" onclick="myFunction()">测试输入的值</button>
<p id="mess" ></p>
</body>
</html>


原创粉丝点击