这个代码不能和我一起使用吗? ,我做了所有的步骤,所以任何人都告诉我我的错在哪里,谢谢你
<html>
<body>
<script>
function myFunction()
{
try
{
var x=document.getElementById("demo").value;
if(x=="") throw="empty";
if(isNaN(x)) throw"not a number";
if(x>10) throw"too high";
if(x<5) throw"too low";
}
catch(err)
{
var y=document.getElementById("mess");
y.innerHTML="Error:" + err + ".";
}
}
</script>
<h1>My First JavaScript</h1>
<p>Please input a number between 5 and 10:</p>
<input id="demo" type="text" />
<button type="button" onclick="myFunction()">Test input</button>
<p id="mess"></p>
</body>
</html>
答案 0 :(得分:2)
您的=
声明中似乎未正确添加throw
,
if(x=="") throw="empty";
删除它然后尝试。
这是工作小提琴:https://jsfiddle.net/74suvvfq/
希望这有帮助!
答案 1 :(得分:0)
$("button").click(function() {
try {
var x=document.getElementById("demo").value;
if(x=="") throw "empty";
if(isNaN(x)) throw "not a number";
if(x>10) throw "too high";
if(x<5) throw "too low";
}
catch(err) {
var y=document.getElementById("mess");
y.innerHTML="Error:" + err + ".";
}
});
希望这会有所帮助