進階 JavaScript 執行個體03

來源:互聯網
上載者:User
簡單的計時
<html><head><script type="text/javascript">function timedMsg(){var t=setTimeout("alert('5 秒!')",5000)}</script></head><body><form><input type="button" value="顯示定時的警告框" onClick = "timedMsg()"></form><p>請點擊上面的按鈕。警告框會在 5 秒後顯示。</p></body></html>

另一個簡單的計時
<html><head><script type="text/javascript">function timedText(){var t1=setTimeout("document.getElementById('txt').value='2 秒'",2000)var t2=setTimeout("document.getElementById('txt').value='4 秒'",4000)var t3=setTimeout("document.getElementById('txt').value='6 秒'",6000)}</script></head><body><form><input type="button" value="顯示計時的文本" onClick="timedText()"><input type="text" id="txt"></form><p>點擊上面的按鈕。輸入框會顯示出已經逝去的時間(2、4、6 秒)。</p></body></html>

在一個無窮迴圈中的計時事件
<html><head><script type="text/javascript">var c=0var tfunction timedCount(){document.getElementById('txt').value=cc=c+1t=setTimeout("timedCount()",1000)}</script></head><body><form><input type="button" value="開始計時!" onClick="timedCount()"><input type="text" id="txt"></form><p>請點擊上面的按鈕。輸入框會從 0 開始一直進行計時。</p></body></html>

帶有停止按鈕的無窮迴圈中的計時事件
<html><head><script type="text/javascript">var c=0var tfunction timedCount(){document.getElementById('txt').value=cc=c+1t=setTimeout("timedCount()",1000)}function stopCount(){clearTimeout(t)}</script></head><body><form><input type="button" value="開始計時!" onClick="timedCount()"><input type="text" id="txt"><input type="button" value="停止計時!" onClick="stopCount()"></form><p>請點擊上面的“開始計時”按鈕來啟動計時器。輸入框會一直進行計時,從 0 開始。點擊“停止計時”按鈕可以終止計時。</p></body></html>

使用計時事件製作的鐘錶
<html><head><script type="text/javascript">function startTime(){var today=new Date()var h=today.getHours()var m=today.getMinutes()var s=today.getSeconds()// add a zero in front of numbers<10m=checkTime(m)s=checkTime(s)document.getElementById('txt').innerHTML=h+":"+m+":"+st=setTimeout('startTime()',500)}function checkTime(i){if (i<10)   {i="0" + i}  return i}</script></head><body onload="startTime()"><div id="txt"></div></body></html>

建立對象的執行個體

 

<html><body><script type="text/javascript">personObj=new Object()personObj.firstname="John"personObj.lastname="Adams"personObj.age=35personObj.eyecolor="black"document.write(personObj.firstname + " 的年齡是 " + personObj.age + " 歲。")</script></body></html>

 

建立用於對象的模板

 

<html><body><script type="text/javascript">function person(firstname,lastname,age,eyecolor){this.firstname=firstnamethis.lastname=lastnamethis.age=agethis.eyecolor=eyecolor}myFather=new person("John","Adams",35,"black")document.write(myFather.firstname + " 的年齡是 " + myFather.age + " 歲。")</script></body></html>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.