JavaScript笔记_2

4.JavaScript 注释:
--单行的注释以 // 开始。

<script type="text/javascript">
// 这行代码输出标题:
document.write("<h1>This is a header</h1>");
// 这行代码输出段落:
document.write("<p>This is a paragraph</p>");
document.write("<p>This is another paragraph</p>");
</script>

 --多行注释以 /* 开头,以 */ 结尾。

<script type="text/javascript">
/*
下面的代码将输出
一个标题和两个段落
*/
document.write("<h1>This is a header</h1>");
document.write("<p>This is a paragraph</p>");
document.write("<p>This is another paragraph</p>");
</script>

 

5.JavaScript 变量:
JavaScript 变量名称的规则:
--变量对大小写敏感;
--变量必须以字母或下划线开始 ;
可以通过 var 语句来声明 JavaScript 变量:

var a=3;
var str="Hello";

 
6.JavaScript If...Else 语句:

<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>

 Switch 语句:

<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.

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 looking forward to this weekend!")
}
</script>

 

你可能感兴趣的:(JavaScript)