JavaScript中的註釋html
(1)單行註釋,如: spa
// 輸出標題:
document.getElementById("myH1").innerHTML="Welcome to my Homepage";
// 輸出段落: document.getElementById("myP").innerHTML="This is my first paragraph.";
(2)多行註釋,如:code
/*
下面的這些代碼會輸出
一個標題和一個段落
並將表明主頁的開始
*/
document.getElementById("myH1").innerHTML="Welcome to my Homepage";
document.getElementById("myP").innerHTML="This is my first paragraph.";
註釋的其餘做用:阻止代碼執行,如htm
//document.getElementById("myH1").innerHTML="Welcome to my Homepage"; document.getElementById("myP").innerHTML="This is my first paragraph.";
或
/* document.getElementById("myH1").innerHTML="Welcome to my Homepage"; document.getElementById("myP").innerHTML="This is my first paragraph."; */
JavaScript變量 變量是存儲信息的容器
變量的命名:
1 必須使用字母開頭
2 變量名稱大小寫敏感
變量的建立:
使用var關鍵字
變量的賦值
var = value;
如:
<!DOCTYPE html>
<html>
<body>ip
<script>
var pi;get
pi=3.14;
var PI=3.1415926;
var name="Bill Gates";
var answer='Yes I am!';it
document.write(pi + "<br>");
document.write(PI + "<br>");
document.write(name + "<br>");
document.write(answer + "<br>");
</script>io
</body>
</html>function
下面是一個簡單的JavaScript算數例子:class
<!DOCTYPE html>
<html>
<body>
<p>假設 y=5,計算 x=y+2,並顯示結果。</p>
<button onclick="myFunction()">點擊這裏</button>
<p id="demo"></p>
<script>
function myFunction()
{
var y=5;
var x=y+2;
var sum=document.getElementById("demo")
sum.innerHTML="x=" + x;
}
</script>
</body>
</html>