使用if....else....处理条件不成立的情形:
使用witch语句进行进行多重条件判断:
使用do...while循环:
continue语句:停止当前循环进入下一次循环:
给函数传递参数:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
var points=prompt("请输入学生成绩:",0);
if(points>=90){
alert("A");
}else if(points>=75){
alert("B");
}else if(points>=60){
alert("C");
}else if(points<60){
alert("D");
}
//-->
</script>
</body>
</html>
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
var points=prompt("请输入学生成绩:",0);
if(points>=90){
alert("A");
}else if(points>=75){
alert("B");
}else if(points>=60){
alert("C");
}else if(points<60){
alert("D");
}
//-->
</script>
</body>
</html>
使用witch语句进行进行多重条件判断:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
var month=prompt("请输入月份:",1);
month=parseInt(month); //非必须:将月份转换为数字类型
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
alert(month+"月有31天");
break;
case 4:
case 6:
case 9:
case 11:
alert(month+"月有30天");
break;
case 2:
alert(month+"月有28天");
break;
default:
alert("error");
break;
}
//-->
</script>
</body>
</html>
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
var month=prompt("请输入月份:",1);
month=parseInt(month); //非必须:将月份转换为数字类型
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
alert(month+"月有31天");
break;
case 4:
case 6:
case 9:
case 11:
alert(month+"月有30天");
break;
case 2:
alert(month+"月有28天");
break;
default:
alert("error");
break;
}
//-->
</script>
</body>
</html>
使用do...while循环:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
do{
var year=prompt("请输入要判断是否为闰年的年份:",0);
if(null==year)break; //如果单击了取消按钮则直接退出
year=parseInt(year); //可选:将year转换为数字类型
var check4=(0==year%4); //判断年份是否能被4整除
var check100=(0==year%100); //判断年份是否能被100整除
var check400=(0==year%400); //判断年份是否能被400整除
//给出判断结果
if(year!=0 && ((check4 && !check100) || check400))alert(year+"年是闰年。");
else if(year!=0)alert(year+"年不是闰年。");
}while(year!=0)
//-->
</script>
</body>
</html>
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
do{
var year=prompt("请输入要判断是否为闰年的年份:",0);
if(null==year)break; //如果单击了取消按钮则直接退出
year=parseInt(year); //可选:将year转换为数字类型
var check4=(0==year%4); //判断年份是否能被4整除
var check100=(0==year%100); //判断年份是否能被100整除
var check400=(0==year%400); //判断年份是否能被400整除
//给出判断结果
if(year!=0 && ((check4 && !check100) || check400))alert(year+"年是闰年。");
else if(year!=0)alert(year+"年不是闰年。");
}while(year!=0)
//-->
</script>
</body>
</html>
continue语句:停止当前循环进入下一次循环:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
while(true){
var year=prompt("请输入要判断是否为闰年的年份:",0);
if(null==year)break; //如果单击了取消按钮则直接退出
if(!/[^0]\d+/.test(year)){
//通过正则表达式判断输入年份的合法性
alert("请输入合法的年份!");
continue;
}
year=parseInt(year); //可选:将year转换为数字类型
var check4=(0==year%4); //判断年份是否能被4整除
var check100=(0==year%100); //判断年份是否能被100整除
var check400=(0==year%400); //判断年份是否能被400整除
//给出判断结果
if( (check4 && !check100) || check400)alert(year+"年是闰年。");
else alert(year+"年不是闰年。");
}
//-->
</script>
</body>
</html>
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
while(true){
var year=prompt("请输入要判断是否为闰年的年份:",0);
if(null==year)break; //如果单击了取消按钮则直接退出
if(!/[^0]\d+/.test(year)){
//通过正则表达式判断输入年份的合法性
alert("请输入合法的年份!");
continue;
}
year=parseInt(year); //可选:将year转换为数字类型
var check4=(0==year%4); //判断年份是否能被4整除
var check100=(0==year%100); //判断年份是否能被100整除
var check400=(0==year%400); //判断年份是否能被400整除
//给出判断结果
if( (check4 && !check100) || check400)alert(year+"年是闰年。");
else alert(year+"年不是闰年。");
}
//-->
</script>
</body>
</html>
给函数传递参数:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>改变网页背景色</title>
</head>
<body>
<input id="btn" type="button" value="改变网页背景色"/>
</body>
</html>
<script language="JavaScript" type="text/javascript">
<!--
var btn=document.getElementById("btn");
btn.onclick=function(){
var color=prompt("请输入颜色名称或颜色代码:","");
changeBgColor(color);
}
function changeBgColor(color){
document.bgColor=color;
}
//-->
</script>
<html>
<head>
<title>改变网页背景色</title>
</head>
<body>
<input id="btn" type="button" value="改变网页背景色"/>
</body>
</html>
<script language="JavaScript" type="text/javascript">
<!--
var btn=document.getElementById("btn");
btn.onclick=function(){
var color=prompt("请输入颜色名称或颜色代码:","");
changeBgColor(color);
}
function changeBgColor(color){
document.bgColor=color;
}
//-->
</script>