菜鸟学习javascript实例教程(2)

2008-02-23 07:41:41来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

<p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
</p>

</body>
</html>

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>")
}
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 r=Math.random()
if (r>0.5)
{
document.write("<a href='http://www.w3schools.com'>Learn Web Development!</a>")
}
else
{
document.write("<a href='http://www.refsnesdata.no'>Visit Refsnes Data!</a>")
}
</script>

</body>
</html>

多条件的语句实现的例子:

<html>
<body>
<script type="text/javascript">
var d = new Date()
theDay=d.getDay()
switch (theDay)
{
case 5:
document.write("Finally Friday")
break
case 6:
document.write("Super Saturday")
break
case 0:
document.write("Sleepy Sunday")
break
default:
document.write("I'm really looking forward to this weekend!")
}
</script>

<p>This example demonstrates the switch statement.</p>

<p>You will receive a different greeting based on what day it is.</p>

<p>Note that Sunday=0, Monday=1, Tuesday=2, etc.</p>

</body>
</html>


循环的例子:

for循环的一个例子:

<html>
<body>

<script type="text/javascript">
for (i = 0; i <= 5; i )
{
document.write("The number is " i)
document.write("<br>")
}
</script>

<p>Explanation:</p>

<p>The for loop sets <b>i</b> equal to 0.</p>

<p>As long as <b>i</b> is less than , or equal to, 5, the loop will continue to run.</p>

<p><b>i</b> will increase by 1 each time the loop runs.</p>

</body>
</html>

复杂循环的一个例子:

<html>
<body>

<script type="text/javascript">
for (i = 1; i <= 6; i )
{
document.write("<h" i ">This is header " i)
document.write("</h" i ">")
}
</script>

</body>
</html>

按条件循环while的例子:

<html>
<body>

<script type="text/javascript">
i = 0
while (i <= 5)
{
document.write("The number is " i)
document.write("<br>")
i
}
</script>

<p>Explanation:</p>

<p><b>i</b> equal to 0.</p>

<p>While <b>i</b> is less than , or equal to, 5, the loop will continue to run.</p>

<p><b>i</b> will increase by 1 each time the loop runs.</p>

</body>
</html>

do...while循环的例子:

<html>
<body>

<script type="text/javascript">
i = 0
do
{
document.write("The number is " i)
document.write("<br>")
i
}
while (i <= 5)
</script>

<p>Explanation:</p>

<p><b>i</b> equal to 0.</p>

<p>The loop will run</p>

<p><b>i</b> will increase by 1 each time the loop runs.</p>

<p>While <b>i</b> is less than , or equal to, 5, the loop will continue to run.</p>


</body>
</html>


字符串对象的例子:

检测字符串长度的例子:

<html>
<body>

<script type="text/javascript">
var str="W3Schools is great!"
document.write("<p>" str "</p>")
document.write(str.length)
</script>

</body>
</html>


检测子字符串位置的例子:

<html>
<body>

<script type="text/javascript">
var str="W3Schools is great!"
var pos=str.indexOf("School")
if (pos>=0)
{
document.write("School found at position: ")
document.write(pos "<br />")
}
else
{
document.write("School not found!")
}
</script>

<p>This example tests if a string contains a specified word. If the word is found it returns the position of the first character of the word in the original string. Note: The first position in the string is 0!</p>

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:网页制作中表单相关特效整理

下一篇:使用Javascript创建XML文件