抛出异常中的java脚本错误

时间:2017-05-09 11:15:38

标签: javascript exception throw

当我输入一些数字时,它不会显示任何数字。我试图在我的site上插入此代码,但无法正常工作。请帮帮我

function myFunction() {
    var message, x;
    message = document.getElementBy("message");
    message.innerHTML = "";
    x = document.getElementById("demo").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;
    }
}
<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="message"></p>

1 个答案:

答案 0 :(得分:0)

你应该说,你还没有正确选择元素  message = document.getElementById("message");