tb codeword:dreamwea中if...else的使用,要放在vbscript中吗?举一个例子!

来源:百度文库 编辑:中科新闻网 时间:2024/04/28 03:20:35

使用 If...Then...Else 进行判断

If...Then...Else 语句用于计算条件是否为 True 或 False,并且根据计算结果指定要运行的语句。通常,条件是使用比较运算符对值或变量进行比较的表达式。有关比较运算符的详细信息,请参阅比较运算符。If...Then...Else 语句可以按照需要进行嵌套。
条件为 True 时运行语句

要在条件为 True 时运行单行语句,可使用 If...Then...Else 语句的单行语法。下例示范了单行语法。请注意此例省略了关键字 Else。

Sub FixDate()
Dim myDate
myDate = #2/13/95#
If myDate < Now Then myDate = Now
End Sub

要运行多行代码,必须使用多行(或块)语法。多行(或块)语法包含 End If 语句,如下所示:

Sub AlertUser(value)
If value = 0 Then
AlertLabel.ForeColor = vbRed
AlertLabel.Font.Bold = True
AlertLabel.Font.Italic = True
End If
End Sub

条件为 True 和 False 时分别运行某些语句

可以使用 If...Then...Else 语句定义两个可执行语句块:条件为 True 时运行某一语句块,条件为 False 时运行另一语句块。

Sub AlertUser(value)
If value = 0 Then
AlertLabel.ForeColor = vbRed
AlertLabel.Font.Bold = True
AlertLabel.Font.Italic = True
Else
AlertLabel.Forecolor = vbBlack
AlertLabel.Font.Bold = False
AlertLabel.Font.Italic = False
End If
End Sub

对多个条件进行判断

If...Then...Else 语句的一种变形允许您从多个条件中选择,即添加 ElseIf 子句以扩充 If...Then...Else 语句的功能,使您可以控制基于多种可能的程序流程。例如:

Sub ReportValue(value)
If value = 0 Then
MsgBox value
ElseIf value = 1 Then
MsgBox value
ElseIf value = 2 then
Msgbox value
Else
Msgbox "数值超出范围!"
End If
End Sub

可以添加任意多个 ElseIf 子句以提供多种选择。使用多个 ElseIf 子句经常会变得很累赘。在多个条件中进行选择的更好方法是使用 Select Case 语句。

----------------------------------

<head>
<title>测试</title>
<script language=vbs>
cont=1
sub myButton_Onclick()
if mytext.value="ntcComCn" then location="http://www.ntc.com.cn" else msgbox "密码错误!正确密码为ntcComCn":cont=cont+1
if cont>3 then msgbox"对不起!本页内容你不能看。":window.close
end sub
</script>
</head>
<body>
<br>
<br>
<center>请输入密码:
<input name="myText" type="password" size=10 maxlength=8>

<input type="button" name="myButton" value="确定" >
</center>
</body>
</html>