通过输入任何值,错误的输出不会改变。 (尝试,抓住,最后)
By entering any value, the output of error does not change. (try , catch ,finally)
我刚开始学习JavaScript,现在我又遇到了错误的问题。通过输入任何值,输出不会改变。请帮忙找出我的代码问题。
function checker(){
var message, x;
message = document.getElementById('resault');
x = document.getElementById('v').Value;
try {
if (x == "") throw "empty";
if (isNaN(x)) throw "not a number"
x = Number(x);
if (x < 5) throw "too low"
if (x > 10) throw "too high"
}
catch(err) {
message.innerHTML= "input is" + err ;
}
finally {
document.getElementById('v').Value = "it is used !"
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>input a number between 5 and 10 ! </p>
<input type="number" id="v" placeholder="input here!">
<button onclick="checker()">check my number</button>
<p id="resault"></p>
<script src="js.js"></script>
</body>
</html>
我想你的意思是它只显示 'input is not a number' 对吧?。尝试将输入类型更改为数字。比它应该工作
我刚开始学习JavaScript,现在我又遇到了错误的问题。通过输入任何值,输出不会改变。请帮忙找出我的代码问题。
function checker(){
var message, x;
message = document.getElementById('resault');
x = document.getElementById('v').Value;
try {
if (x == "") throw "empty";
if (isNaN(x)) throw "not a number"
x = Number(x);
if (x < 5) throw "too low"
if (x > 10) throw "too high"
}
catch(err) {
message.innerHTML= "input is" + err ;
}
finally {
document.getElementById('v').Value = "it is used !"
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>input a number between 5 and 10 ! </p>
<input type="number" id="v" placeholder="input here!">
<button onclick="checker()">check my number</button>
<p id="resault"></p>
<script src="js.js"></script>
</body>
</html>
我想你的意思是它只显示 'input is not a number' 对吧?。尝试将输入类型更改为数字。比它应该工作