DIV CSS 佈局教程網

JavaScript 基礎知識(三):BOM 與 DOM 重點整理
編輯:JavaScript基礎知識     

window 物件

開啟與關閉視窗

window.open(url, name, features)

三個參數分別是:載入新視窗的頁面 URL、新視窗的名稱,以及特性字串(並可指定是否用新載入的頁面取代目前頁面)。關閉視窗則用:

window.close()

系統對話框

alert("hello world!");            // 只有一個按鈕的提示框
confirm("Are you sure?");         // 確認/取消,回傳布林值
prompt("你的名字是什麼?", "");    // 帶輸入框的提示,回傳輸入內容

計時器

setTimeout("alert('hello world!')", 3000);   // 3 秒後執行一次
clearTimeout(id);                             // 取消計時器

setInterval("alert('hello world')", 3000);    // 每 3 秒執行一次
clearInterval(id);                            // 取消週期執行

歷史紀錄

window.history.go(-1);      // 回到上一頁
window.history.back();      // 回到上一頁
window.history.go(1);       // 前進一頁
window.history.forward();   // 前進一頁
window.history.length;      // 查詢歷史中的頁面數

document 物件

document.links[0];                        // 存取連結
document.images[0];                       // 存取圖像
document.images["imgHome"];
document.forms[0];                        // 存取表單
document.forms["frmSubscribe"];

location 物件

href 屬性代表目前載入頁面的完整 URL,可讀也可寫;賦值後瀏覽器會跳轉並載入新文件。

DOM 的主要節點類型

  1. 元素節點(Element)
  2. 屬性節點(Attributes)
  3. 文本節點(Text)
  4. 註解節點(Comment)

所有節點通用的方法與屬性

查找

userData.nodeName    // 節點名稱
nodeValue            // 節點的值,主要用於文本節點;元素節點回傳 null
nodeType             // 節點類型
firstChild           // 第一個子節點
lastChild            // 最後一個子節點
childNodes           // 所有子節點的集合
previousSibling      // 前一個兄弟節點(同層)
nextSibling          // 後一個兄弟節點(同層)

操作

attributes     // 包含代表元素各項特性的 Attr 物件,僅用於 Element 節點
appendChild    // 在最後新增一個節點
removeChild    // 刪除一個節點
replaceChild   // 替換節點
insertBefore   // 在指定節點之前插入

建立

createElement    // 建立元素節點
createTextNode   // 建立文本節點

複製

cloneNode(true);    // 深度複製,連子節點一起複製
cloneNode(false);   // 淺度複製,只複製自身

存取指定節點

getElementById        // 依指定的 id 查找
getElementsByTagName  // 依標籤名查找,回傳陣列形式
getElementsByName     // 依 name 屬性查找

屬性的查找、修改與設定

getAttribute()      // 查找屬性
setAttribute()      // 修改或設定屬性,例如 setAttribute("class", "red")
removeAttribute()   // 刪除屬性

覆寫與新增文字內容

使用 innerHTML 可以直接覆寫或新增元素內部的 HTML 內容,是最簡便的做法。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved