?? javascript2.htm
字號:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
a{ text-decoration: none }
p{ line-height:140% }
.text{ font-size: 9pt }
.text{ text-align:justify }
.text11{ font-size: 11pt }
.text11{ text-align:justify }
-->
</style>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<title>網頁教程-JavaScript技術講座:第二講 JavaScript基本數據結構 </title>
</head>
<body>
<p align="center">
</p>
<p><span class="text">您現在的位置:<a href="javascript:if(confirm('http://www.4gee.com/Index.htm \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.4gee.com/Index.htm'" tppabs="http://www.4gee.com/Index.htm">主頁</a>-<a
href="javascript:if(confirm('http://www.4gee.com/colleges/Index.htm \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.4gee.com/colleges/Index.htm'" tppabs="http://www.4gee.com/colleges/Index.htm">網絡教室</a>-<a href="Index.htm" tppabs="http://www.4gee.com/colleges/homepages/Index.htm">主頁工廠</a>-JavaScript技術講座:第二講
JavaScript基本數據結構</span></p>
<p align="center"><small><a href="JavaScript1.htm" tppabs="http://www.4gee.com/colleges/homepages/JavaScript1.htm">上一頁</a>
<a href="JavaScript3.htm" tppabs="http://www.4gee.com/colleges/homepages/JavaScript3.htm">下一頁</a></small></p>
<table border="0" width="100%">
<tr>
<td width="100%"><p align="center"><span style="color: rgb(252,177,3)" class="text11">JavaScript技術講座<br>
第二講 JavaScript基本數據結構</span></td>
</tr>
</table>
<table border="0" width="100%">
<tr>
<td width="100%"><span style="color: rgb(125,125,125)" class="text">
JavaScript提供腳本語言的編程與C++非常相似,它只是去掉了C語言中有關指針等容易產生的錯誤,并提供了功能強大的類庫。對于已經具備C++或C語言的人來說,學習JavaScript腳本語言是一件非常輕松愉快的事。</span><p><span
style="color: rgb(125,125,125)" class="text"><font color="#009933">一、JavaScript代碼的加入</font></span></p>
<p><span style="color: rgb(125,125,125)" class="text">JavaScript的腳本包括在HTML中,它成為HTML文檔的一部分。與HTML標識相結合,構成了一個功能強大的Internet網上編程語言。可以直接將JavaScript腳本加入文檔:</span></p>
<p><span style="color: rgb(125,125,125)" class="text"><Script Language
="JavaScript"></span></p>
<p><span style="color: rgb(125,125,125)" class="text">JavaScript語言代碼;</span></p>
<p><span style="color: rgb(125,125,125)" class="text">JavaScript 語言代碼; </span></p>
<p><span style="color: rgb(125,125,125)" class="text">....</span></p>
<p><span style="color: rgb(125,125,125)" class="text"></Script></span></p>
<p><span style="color: rgb(125,125,125)" class="text">說明:</span></p>
<p><span style="color: rgb(125,125,125)" class="text"> 通過標識<Script>...</Script>指明JavaScript腳本源代碼將放入其間。</span></p>
<p><span style="color: rgb(125,125,125)" class="text"> 通過屬性Language
="JavaScript"說明標識中是使用的何種語言,這里是JavaScript語言,
表示在JavaScript中使用的語言。</span></p>
<p> </p>
<p><span style="color: rgb(125,125,125)" class="text">下面是將JavaScript腳本加入Web文檔中的例子:</span></p>
<p><span style="color: rgb(125,125,125)" class="text">Test2.html</span></p>
<p><span style="color: rgb(125,125,125)" class="text"><HTML></span></p>
<p><span style="color: rgb(125,125,125)" class="text"><Head></span></p>
<p><span style="color: rgb(125,125,125)" class="text"><Script Language
="JavaScript"></span></p>
<p><span style="color: rgb(125,125,125)" class="text">document. Write("這是電腦報網絡學校");</span></p>
<p><span style="color: rgb(125,125,125)" class="text">document. close();</span></p>
<p><span style="color: rgb(125,125,125)" class="text"></Script></span></p>
<p><span style="color: rgb(125,125,125)" class="text"></Head></span></p>
<p><span style="color: rgb(125,125,125)" class="text"></HTML></span></p>
<p><span style="color: rgb(125,125,125)" class="text"> 在瀏覽器的窗口中調用test2.html,則顯示“這是電腦報網絡學校”字串。見圖2所示。</span></p>
<p><img src="javascript/002.gif" tppabs="http://www.4gee.com/colleges/homepages/javascript/002.gif" width="212" height="230" alt="002.gif (7229 字節)"></p>
<p> </p>
<p><span style="color: rgb(125,125,125)" class="text">圖2 </span></p>
<p><span style="color: rgb(125,125,125)" class="text">說明:</span></p>
<p><span style="color: rgb(125,125,125)" class="text"> Document. write()是文檔對象的輸出函數,其功能是將括號中的字符或變量值輸出到窗口;document.
close()是將輸出關閉。</span></p>
<p><span style="color: rgb(125,125,125)" class="text"> 可將<Script>...</Script>標識放入head>..
</Head>或<Body> ...</Body>之間。將JavaScript標識放置<Head>...
</Head>在頭部之間,使之在主頁和其余部分代碼之前裝載,從而可使代碼的功能更強大;可以將JavaScript標識放置在<Body>...
</Body>主體之間以實現某些部分動態地創建文檔。</span></p>
<p><span style="color: rgb(125,125,125)" class="text"><font color="#009933">二、基本數據類型
</font></span></p>
<p><span style="color: rgb(125,125,125)" class="text">JavaScript腳本語言同其它語言一樣,有它自身的基本數據類型、表達式和算術運算符以及程序的基本框架結構。JavaScript提供了四種基本的數據類型用來處理數字和文字,
而變量提供存放信息的地方, 表達式則可以完成較復雜的信息處理。</span></p>
<p><span style="color: rgb(125,125,125)" class="text">1、基本數據類型</span></p>
<p><span style="color: rgb(125,125,125)" class="text">在JavaScript中四種基本的數據類型:數值(整數和實數)、字符串型(用“”號或‘’括起來的字符或數值)、布爾型(使True或False表示)和空值。在JavaScript的基本類型中的數據可以是常量,也可以變量。由于JavaScript采用弱類型的形式,因而一個數據的變量或常量不必首先作聲明,而是在使用或賦值時確定其數據的類型的。當然也可以先聲明該數據的類型,它是通過在賦值時自動說明其數據類型的。</span></p>
<p><span style="color: rgb(125,125,125)" class="text">2、常量</span></p>
<p><span style="color: rgb(125,125,125)" class="text"> 整型常量</span></p>
<p><span style="color: rgb(125,125,125)" class="text">JavaScript的常量通常又稱字面常量,它是不能改變的數據。其整型常量可以使用十六進制、八進制和十進制表示其值。</span></p>
<p><span style="color: rgb(125,125,125)" class="text"> 實型常量</span></p>
<p><span style="color: rgb(125,125,125)" class="text">實型常量是由整數部分加小數部分表示,如12.32、193.98
。可以使用科學或標準方法表示:5E7、4e5等。</span></p>
<p><span style="color: rgb(125,125,125)" class="text"> 布爾值</span></p>
<p><span style="color: rgb(125,125,125)" class="text">布爾常量只有兩種狀態:True或False。
它主要用來說明或代表一種狀態或標志,以說明操作流程。它與C++是不一樣的,C++可以用1或0表示其狀態,而JavaScript只能用True或False表示其狀態。</span></p>
<p><span style="color: rgb(125,125,125)" class="text"> 字符型常量</span></p>
<p><span style="color: rgb(125,125,125)" class="text">使用單引號(‘)或雙引號(“)括起來的一個或幾個字符。如
"This is a book of JavaScript "、"3245"、"ewrt234234"
等。</span></p>
<p><span style="color: rgb(125,125,125)" class="text"> 空值</span></p>
<p><span style="color: rgb(125,125,125)" class="text">JavaScript中有一個空值null,表示什么也沒有。如試圖引用沒有定義的變量,則返回一個Null值。</span></p>
<p><span style="color: rgb(125,125,125)" class="text"> 特殊字符</span></p>
<p><span style="color: rgb(125,125,125)" class="text">同C語言一樣,JavaScript中同樣以有些以反斜杠(/)開頭的不可顯示的特殊字符。通常稱為控制字符。</span></p>
<p> </p>
<p><span style="color: rgb(125,125,125)" class="text">3、變量</span></p>
<p><span style="color: rgb(125,125,125)" class="text">變量的主要作用是存取數據、提供存放信息的容器。對于變量必須明確變量的命名、變量的類型、變量的聲明及其變量的作用域。</span></p>
<p><span style="color: rgb(125,125,125)" class="text"> 變量的命名</span></p>
<p><span style="color: rgb(125,125,125)" class="text">JavaScript中的變量命名同其計算機語言非常相似,這里要注意以下兩點:</span></p>
<p><span style="color: rgb(125,125,125)" class="text">A、必須是一個有效的變量,即變量以字母開頭,中間可以出現數字如test1、text2等。除下劃線(-)作為連字符外,變量名稱不能有空格、(+)、(-)、(,)或其它符號。</span></p>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -