function total(numberA,numberB) { return numberA + numberB } </script>
</head> <body>
<script type="text/javascript"> document.write(total(2,3)) </script>
<p>The script in the body section calls a function with two arguments, 2 and 3.</p>
<p>The function returns the sum of these two arguments.</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>") } </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>
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>
上一篇:网页制作中表单相关特效整理
下一篇:javascript的键盘控制事件
|