(一)資料型別與變數型別:整數、小數、字串、日期時間、陣列。強制轉換可用 parseInt()、parseFloat()、isNaN()。
(二)陣列:var 陣列名 = new Array([長度]);,用 a.length 取得長度,用 a[下標] 存取元素。
(三)函式:
function 函式名(形參)
{
}
DOM 是 Document Object Model(文件物件模型)的縮寫,它用樹狀模型來表示文件的結構。從 window 出發,往下有 history、location、document 等物件,document 底下又有 html、head、body 以及 a、img、table、ul 等元素。
物件(object)的特點是名詞代表屬性、動詞代表行為。以下介紹幾個 window 的常用方法:
1、alert():彈出警示視窗。
2、confirm():彈出確認視窗。
3、open():在新視窗中開啟頁面,window.open("https://hovertree.com") 會回傳一個 window 型別的變數。
4、close():關閉視窗。
5、setTimeout():在指定的毫秒數後執行 code 一次。
以下程式碼示範每隔一秒更新一次時間:
function showTime()
{
var date = new Date();
document.getElementById("hh").innerHTML = date;
window.setTimeout("showTime()",1000);
}
window.setTimeout("showTime()",1000);
再來看一個「5 秒後彈出視窗、再過 5 秒自動關閉」的範例:
var a;
function openAD()
{
a = window.open("https://hovertree.com","_blank","width=200 height=200 toolbar=no top=0 left=0");
window.setTimeout("closeAD()",5000);
}
function closeAD()
{
a.close();
}
window.setTimeout("openAD()",5000);
透過 setTimeout() 搭配自呼叫,就能靈活實作各種定時與延遲的效果。