Javascript 错误处理:try throw catch

来源:互联网 发布:union mysql性能级别 编辑:程序博客网 时间:2024/06/05 00:44
<body>    <p>请输入1~10的数字:</p>    <input id="input" type="text" onfocus="clearAll(this)">    <input type="button" value="校验" onclick = "check()">    <p id="msg"></p></body>
<script>    function check(){        try{            var x = document.getElementById("input").value;            if(x==""){throw "值为空!";}            if(isNaN(x)){throw "不是数字!";}            if(x<1){throw "数字太小!";}            if(x>10){throw "数字太大!";}        }        catch(error){            var y = document.getElementById("msg");            y.innerHTML = "错误:" + error ;        }    }    function clearAll(x){        x.value="";    }</script>
0 0
原创粉丝点击