下面的這段文字是我的代碼 它運作良好,當我輸入空/ NUM低/高NUM 然而,當我輸入字符串它不能很好地工作我不能修復我的錯誤
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Show error message depending on input value by using try/catch/throw</h1>
<input type="text" id="input1">
<button id="btn1" onclick="showR()">Show the result</button>
<p id="demo1"></p>
<script type="text/javascript">
function showR(){
var x;
x= document.getElementById('input1').value;
//x= Number(x);
try{
if(x=="")throw "empty";
if(x==isNaN(x))throw "not a number";
x= Number(x);
if(x<5)throw "too low";
if(x>10)throw "too high";
document.getElementById(demo1).innerHTML=x;
}catch(err){
document.getElementById('demo1').innerHTML = "input is : " + err;
}
document.getElementById('input1').innerHTML='hi';
}
</script>
</body>
</html>
該錯誤信息是 輸入:類型錯誤:無法設置爲null 的特性「的innerHTML」的原因是什麼,我怎麼能解決這個問題
'if(x == isNaN(x))'總是false ...擺脫'x ==' –
下次使用[JSHint](http://jshint.com/)。它爲您節省了大量時間和工作,並減少了提問這類錯字相關問題的需求。 – Xufox