在以下程序中,當只輸入空格時,它會顯示ex2
例外value less than 5
,而不是顯示ex4
例外This is not a valid number
,我無法理解它背後的邏輯。Javascript代碼
<html>
<head>
<title></title>
<script type="text/javascript">
function promptCheck() {
var val=prompt("Enter a Number between 5 and 10","");
try {
if(val=="") {
throw "ex1";
}
else if(val<5) {
throw "ex2";
}
else if(val>10) {
throw "ex3";
}
else if(isNaN(val)) {
throw "ex4";
}
}
catch(err) {
if(err=="ex1") {
alert("You have not entered any value");
}
if(err=="ex2") {
alert("Value less than 5");
}
if(err=="ex3") {
alert("Value greater than 10");
}
if(err=="ex4") {
alert("This is not a valid number");
}
}
}
</script>
</head>
<body>
<input type="button" value="Bring Mouse on Me!" onmouseover="promptCheck()" />
</body>
</html>
是啊...解決了麻煩問題! – sandbox 2012-02-11 14:55:59