第一个JavaScript程序
<html>
<body>
<script>
function myFunction(){
try{
var x = document.getElementById("demo").value;
if(x=="") throw "值为空!";
if(isNaN(x)) throw "不是数字";
if(x<5) throw "值太小";
if(x>10) throw "太大";
else alert("正确");
}catch(err){
var y = document.getElementById("mess");
y.innerHTML="错误:"+err+".";
}
}
</script>
<h1>我的第一个JavaScript程序</h1><br />
<h5>请输入5到10之间的数字</h5>
<input type="text" id="demo"/>
<button type="button" onclick="myFunction()">测试输入值</button>
<p id="mess"></p>
</body>
</html>
关键点:取得input的值–>var x = document.getElementById(“id”).value;