服务热线
153 8323 9821
家好,学习之家—菜鸟变高手(http://www.xxzj51.com)的js实例100又和你网页了,很多语言一样js中也设有判断语句。if else,下面先说一样js中的条件语句的使用方法。
在您编写代码时,经常需要根据不同的条件完成不同的行为。可以在代码中使用条件语句来完成这个任务。
在 JavaScript 中,我们可以使用下面几种条件语句:
1、if语句
如果希望指定的条件成立时执行代码,就可以使用这个语句。
语法:
- if (条件)
- {
- 条件成立时执行代码
- }
注意:注意:请使用小写字母。使用大写的 IF 会出错!
实例1:
- <script type="text/javascript">
- //Write a "Good morning" greeting if
- //the time is less than 10
- var d=new Date()
- var time=d.getHours()
- if (time<10)
- {
- document.write("<b>Good morning</b>")
- }
- </script>
实例2:
- <script type="text/javascript">
- //Write "Lunch-time!" if the time is 11
- var d=new Date()
- var time=d.getHours()
- if (time==11)
- {
- document.write("<b>Lunch-time!</b>")
- }
- </script>
实例3:嵌入html页面中
- <html>
- <body>
- <