2016-10-07 101 views
0

Y此代碼不適用於我? ,我做了所有步驟,所以任何一個PLZ告訴我,是我的錯,並感謝üCatch error not in javascript

<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> 

回答

2

似乎已包含在這裏您throw聲明的=錯誤,

if(x=="") throw="empty"; 

只是刪除它並嘗試。

這裏是工作提琴https://jsfiddle.net/74suvvfq/

希望這有助於!

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 + "."; 
} 
}); 

希望這將有助於