JavaScript學習記錄(進階)

來源:互聯網
上載者:User
文章目錄
  • 執行個體
  • 文法
  • 執行個體
  • 文法
  • 執行個體
  • 1. 建立對象的執行個體
  • 2. 建立對象的模版
Navigator 對象

JavaScript Navigator 對象包含了有關訪問者瀏覽器的所有資訊。接下來我們學習 Navigator 對象的兩個屬性。

appName
儲存瀏覽器類型
appVersion
存有瀏覽器的版本資訊(其他資訊中的一項)
執行個體
<html><body><script type="text/javascript">var browser=navigator.appNamevar b_version=navigator.appVersionvar version=parseFloat(b_version)document.write("Browser name: "+ browser)document.write("<br />")document.write("Browser version: "+ version)</script></body></html>
JavaScript 表單驗證
JavaScript 可用來在資料被送往伺服器前對 HTML 表單中的這些輸入資料進行驗證。
被 JavaScript 驗證的這些典型的表單資料有:
  • 使用者是否已填寫表單中的必填項目?
  • 使用者輸入的郵件地址是否合法?
  • 使用者是否已輸入合法的日期?
  • 使用者是否在資料域 (numeric field) 中輸入了文本?

下面是連同 HTML 表單的完整代碼:

<html><head><script type="text/javascript">function validate_email(field,alerttxt){with (field){apos=value.indexOf("@")dotpos=value.lastIndexOf(".")if (apos<1||dotpos-apos<2){alert(alerttxt);return false}else {return true}}}function validate_form(thisform){with (thisform){if (validate_email(email,"Not a valid e-mail address!")==false){email.focus();return false}}}</script></head><body><form action="submitpage.htm"onsubmit="return validate_form(this);" method="post">Email: <input type="text" name="email" size="30"><input type="submit" value="Submit"></form></body></html>
JavaScript 動畫

使用 JavaScript 建立生動影像是可行的。

竅門是:使用 JavaScript 通過不同的事件來切換不同的映像。

 

<html>
<head>
<script type="text/javascript">
function mouseOver()
{
document.b1.src ="/i/eg_mouse.jpg"
}
function mouseOut()
{
document.b1.src ="/i/eg_mouse2.jpg"
}
</script>
</head>

<body>
<a href="/index.html" target="_blank">
<img border="0" alt="Visit W3School!" src="/i/eg_mouse2.jpg" name="b1"  onmouseover="mouseOver()" onmouseout="mouseOut()" /></a>
</body>
</html>

JavaScript 計時事件

setTimeout()
未來的某時執行代碼
clearTimeout()
取消setTimeout()
setTimeout()文法
var t=setTimeout("javascript語句",毫秒)
執行個體

當下面這個例子中的按鈕被點擊時,一個提示框會在5秒中後彈出。

<html><head><script type="text/javascript">function timedMsg(){var t=setTimeout("alert('5 seconds!')",5000)}</script></head><body><form><input type="button" value="Display timed alertbox!" onClick="timedMsg()"></form></body></html>
clearTimeout()文法
clearTimeout(setTimeout_variable)
執行個體

下面的例子和上面的無窮迴圈的例子相似。唯一的不同是,現在我們添加了一個 "Stop Count!" 按鈕來停止這個計數器:

<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="Start count!" onClick="timedCount()"><input type="text" id="txt"><input type="button" value="Stop count!" onClick="stopCount()"></form></body></html>
1. 建立對象的執行個體

下列代碼建立了一個對象的執行個體,並向其添加了四個屬性:

personObj=new Object()personObj.firstname="John"personObj.lastname="Doe"personObj.age=50personObj.eyecolor="blue"

向 personObj 添加方法也很簡單。下列代碼向 personObj 添加了名為 eat() 的方法:

personObj.eat=eat
2. 建立對象的模版

模版定義了對象的結構。

function person(firstname,lastname,age,eyecolor){this.firstname=firstnamethis.lastname=lastnamethis.age=agethis.eyecolor=eyecolor}

注意:模版僅僅是一個函數。你需要在函數內部向 this.propertiName 分配內容。

一旦擁有模版,你就可以建立新的執行個體,就像這樣:

myFather=new person("John","Doe",50,"blue")myMother=new person("Sally","Rally",48,"green")

同樣可以向 person 對象添加某些方法。並且同樣需要在模版內進行操作:

function person(firstname,lastname,age,eyecolor){this.firstname=firstnamethis.lastname=lastnamethis.age=agethis.eyecolor=eyecolorthis.newlastname=newlastname}

注意:方法只是依附於對象的函數而已。然後,我們需要編寫 newlastname() 函數:

function newlastname(new_lastname){this.lastname=new_lastname}

Newlastname() 函數定義 person 的新的 lastname,並將之分配給 person。通過使用 “this.”,JavaScript 即可得知你指的 person 是誰。因此,現在你可以這樣寫:myMother.newlastname("Doe")。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.