<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
function a()
{
var r=document.getElementById("cs").value;
if(r>10)
{
throw new Error("不能大鱼10");
}
else{
alert("d");
}
}
</script>
</head><body>
<input type="text" id="cs" />
<input type="button" value="按下" onclick="a()" />
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
function a()
{
var r=document.getElementById("cs").value;
if(r>10)
{
throw new Error("不能大鱼10");
}
else{
alert("d");
}
}
</script>
</head><body>
<input type="text" id="cs" />
<input type="button" value="按下" onclick="a()" />
</body>
</html>
代码是没问题的,大于10就抛出错误,这里如果要弹出警告对话框的话就用alert,throw只抛出错误。也可以在后面用catch捕获错误然后进行相应的操作。