JavaScript 是 Web 的編程語言。
輸出文本
- <script>
- document.write(Date());
- </script>
改變HTML元素
- <!DOCTYPE html>
- <html>
- <body>
- <h1>我的第一個 Web 頁面</h1>
- <p id="demo">我的第一個段落。</p>
- <script>
- document.getElementById("demo").innerHTML="段落已修改。";
- </script>
- </body>
- </html>
調(diào)用內(nèi)部函數(shù)
- <!DOCTYPE html>
- <html>
- <body>
-
- <h1>我的 Web 頁面</h1>
-
- <p id="myPar">我是一個段落。</p>
- <div id="myDiv">我是一個div。</div>
-
- <p>
- <button type="button" onclick="myFunction()">點擊這里</button>
- </p>
-
- <script>
- function myFunction()
- {
- document.getElementById("myPar").innerHTML="你好世界";
- document.getElementById("myDiv").innerHTML="你最近怎么樣?";
- }
- </script>
-
- <p>當(dāng)您點擊上面的按鈕時,兩個元素會改變。</p>
-
- </body>
- </html>
調(diào)用外部腳本函數(shù)
- <!DOCTYPE html>
- <html>
- <body>
- <h1>我的 Web 頁面</h1>
- <p id="demo">一個段落。</p>
- <button type="button" onclick="myFunction()">點擊這里</button>
- <p><b>注釋:</b>myFunction 保存在名為 "myScript.js" 的外部文件中。</p>
- <script src="myScript.js"></script>
- </body>
- </html>
變量的使用
- <!DOCTYPE html>
- <html>
- <body>
- <script>
- var firstname;
- firstname="Hege";
- document.write(firstname);
- document.write("<br>");
- firstname="Tove";
- document.write(firstname);
- </script>
- <p>The script above declares a variable,
- assigns a value to it, displays the value, changes the value,
- and displays the value again.</p>
- </body>
- </html>
隨機函數(shù)
Alert警告框
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <script>
- function myFunction()
- {
- alert("你好,我是一個警告框!");
- }
- </script>
- </head>
- <body>
-
- <input type="button" onclick="myFunction()" value="顯示警告框" />
-
- </body>
- </html>
confirm確認(rèn)框
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- </head>
- <body>
-
- <p>點擊按鈕,顯示確認(rèn)框。</p>
-
- <button onclick="myFunction()">點我</button>
-
- <p id="demo"></p>
-
- <script>
- function myFunction()
- {
- var x;
- var r=confirm("按下按鈕!");
- if (r==true)
- {
- x="你按下了\"確定\"按鈕!";
- }
- else
- {
- x="你按下了\"取消\"按鈕!";
- }
- document.getElementById("demo").innerHTML=x;
- }
- </script>
-
- </body>
- </html>
prompt提示框(帶輸入)
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- </head>
- <body>
-
- <p>點擊按鈕查看輸入的對話框。</p>
-
- <button onclick="myFunction()">點我</button>
-
- <p id="demo"></p>
-
- <script>
- function myFunction()
- {
- var x;
-
- var person=prompt("請輸入你的名字","Harry Potter");
- if (person!=null && person!="")
- {
- x="你好 " + person + "! 今天感覺如何?";
- document.getElementById("demo").innerHTML=x;
- }
- }
- </script>
-
- </body>
- </html>
帶參數(shù)的函數(shù)
- <!DOCTYPE html>
- <html>
- <body>
-
- <p>點擊這個按鈕,來調(diào)用帶參數(shù)的函數(shù)。</p>
-
- <button onclick="myFunction('Harry Potter','Wizard')">點擊這里</button>
-
- <script>
- function myFunction(name,job)
- {
- alert("Welcome " + name + ", the " + job);
- }
- </script>
-
- </body>
- </html>
帶參數(shù)和返回值的函數(shù)
- <!DOCTYPE html>
- <html>
- <body>
-
- <p>本例調(diào)用的函數(shù)會執(zhí)行一個計算,然后返回結(jié)果:</p>
-
- <p id="demo"></p>
-
- <script>
- function myFunction(a,b)
- {
- return a*b;
- }
-
- document.getElementById("demo").innerHTML=myFunction(4,3);
- </script>
-
- </body>
- </html>
for循環(huán)
- <!DOCTYPE html>
- <html>
- <body>
-
- <p>Click the button to loop through a block of code five times.</p>
- <button onclick="myFunction()">Try it</button>
- <p id="demo"></p>
-
- <script>
- function myFunction()
- {
- var x="",i;
- for (i=0;i<5;i++)
- {
- x=x + "The number is " + i + "<br>";
- }
- document.getElementById("demo").innerHTML=x;
- }
- </script>
-
- </body>
- </html>
for in遍歷數(shù)組內(nèi)元素
- <!DOCTYPE html>
- <html>
- <body>
- <p>點擊下面的按鈕,循環(huán)遍歷對象 "person" 的屬性。</p>
- <button onclick="myFunction()">點擊這里</button>
- <p id="demo"></p>
-
- <script>
- function myFunction()
- {
- var x;
- var txt="";
- var person={fname:"Bill",lname:"Gates",age:56};
-
- for (x in person)
- {
- txt=txt + person[x];
- }
-
- document.getElementById("demo").innerHTML=txt;
- }
- </script>
- </body>
- </html>
簡單的計時
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- var c=0;
- var t;
- var timer_is_on=0;
-
- function timedCount()
- {
- document.getElementById('txt').value=c;
- c=c+1;
- t=setTimeout(function(){timedCount()},1000);
- }
-
- function doTimer()
- {
- if (!timer_is_on)
- {
- timer_is_on=1;
- timedCount();
- }
- }
-
- function stopCount()
- {
- clearTimeout(t);
- timer_is_on=0;
- }
- </script>
- </head>
-
- <body>
- <form>
- <input type="button" value="Start count!" onclick="doTimer()" />
- <input type="text" id="txt" />
- <input type="button" value="Stop count!" onclick="stopCount()" />
- </form>
- <p>
- Click on the "Start count!" button above to start the timer. The input field will count forever, starting at 0. Click on the "Stop count!" button to stop the counting. Click on the "Start count!" button to start the timer again.
- </p>
- </body>
- </html>
字典
- <!DOCTYPE html>
- <html>
- <body>
-
- <script>
- person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"}
-
- document.write(person.firstname + " is " + person.age + " years old.");
- </script>
-
- </body>
- </html>
創(chuàng)建用于對象的模板
- <!DOCTYPE html>
- <html>
- <body>
-
- <script>
- function person(firstname,lastname,age,eyecolor)
- {
- this.firstname=firstname;
- this.lastname=lastname;
- this.age=age;
- this.eyecolor=eyecolor;
- }
-
- myFather=new person("John","Doe",50,"blue");
-
- document.write(myFather.firstname + " is " + myFather.age + " years old.");
- </script>
-
- </body>
- </html>
|