JavaScript学习与实践(5)
2007-01-21 11:23
399 查看
JS IF.....ELSE语句
条件语句在JS中用于在不同的条件下执行特定的语句块的语句,
下面的例子分别演示如何写一个IF语句,IF...ELSE语句,IF..ELSE IF...ELSE 语句,还有一个随机链接的例子,代码如下
一
<html>
<body>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time < 10)
{
document.write("<b>Good morning</b>")
}
</script>
<p>
This example demonstrates the If statement.
</p>
<p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
</p>
</body>
</html>
二
<html>
<body>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time < 10)
{
document.write("<b>Good morning</b>")
}
else
{
document.write("<b>Good day</b>")
}
</script>
<p>
This example demonstrates the If...Else statement.
</p>
<p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
Otherwise you will get a "Good day" greeting.
</p>
</body>
</html>
三
<html>
<body>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10)
{
document.write("<b>Good morning</b>")
}
else if (time>=10 && time<16)
{
document.write("<b>Good day</b>")
}
else
{
document.write("<b>Hello World!</b>")
}
</script>
<p>
This example demonstrates the if..else if...else statement.
</p>
</body>
</html>
四
<html>
<body>
<script type="text/javascript">
var r=Math.random()
if (r>0.5)
{
document.write("<a href='http://www.sohu.com'>Learn Web Development!</a>")
}
else
{
document.write("<a href='http://www.163.com'>Visit Refsnes Data!</a>")
}
</script>
</body>
</html>
t条件语句
这个是你写代码使用很频繁的语句,根据不同的条件执行特定的代码,在,JS里,你可以根据需要选择IF ,IF..ELSE,if..else if...else,或者是SWITCH语句,
条件语句在JS中用于在不同的条件下执行特定的语句块的语句,
下面的例子分别演示如何写一个IF语句,IF...ELSE语句,IF..ELSE IF...ELSE 语句,还有一个随机链接的例子,代码如下
一
<html>
<body>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time < 10)
{
document.write("<b>Good morning</b>")
}
</script>
<p>
This example demonstrates the If statement.
</p>
<p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
</p>
</body>
</html>
二
<html>
<body>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time < 10)
{
document.write("<b>Good morning</b>")
}
else
{
document.write("<b>Good day</b>")
}
</script>
<p>
This example demonstrates the If...Else statement.
</p>
<p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
Otherwise you will get a "Good day" greeting.
</p>
</body>
</html>
三
<html>
<body>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10)
{
document.write("<b>Good morning</b>")
}
else if (time>=10 && time<16)
{
document.write("<b>Good day</b>")
}
else
{
document.write("<b>Hello World!</b>")
}
</script>
<p>
This example demonstrates the if..else if...else statement.
</p>
</body>
</html>
四
<html>
<body>
<script type="text/javascript">
var r=Math.random()
if (r>0.5)
{
document.write("<a href='http://www.sohu.com'>Learn Web Development!</a>")
}
else
{
document.write("<a href='http://www.163.com'>Visit Refsnes Data!</a>")
}
</script>
</body>
</html>
t条件语句
这个是你写代码使用很频繁的语句,根据不同的条件执行特定的代码,在,JS里,你可以根据需要选择IF ,IF..ELSE,if..else if...else,或者是SWITCH语句,
相关文章推荐
- JavaScript学习之事件原理和实践
- 【学习笔记javascript设计模式与开发实践----1】
- 《javascript设计模式与开放实践》学习(一)javascript实现多态1
- 【学习笔记javascript设计模式与开发实践(发布--订阅模式)----8】
- 【学习笔记javascript设计模式与开发实践(状态模式)----16】
- 【学习笔记javascript设计模式与开发实践(适配器模式)----17】
- JavaScript学习与实践(6)
- JavaScript学习与实践(11)
- JavaScript学习与实践(17)
- JavaScript学习与实践(19)
- JavaScript学习与实践(21)
- 前端学习笔记--javascript--实践
- 学习JavaScript最佳实践方法
- 【学习笔记javascript设计模式与开发实践(命令模式)----9】
- 《javascript设计模式与开放实践》学习(一)javascript实现多态2
- javascript学习与实践(1)
- javascript学习与实践(2)
- JavaScript学习与实践(12)
- JavaScript学习与实践(18)
- JavaScript学习与实践(22)