DIV CSS 佈局教程網

JavaScript window.document 屬性、方法與事件完整整理
編輯:JavaScript基礎知識     

document 是 window 的子物件,也是存取頁面所有元素的入口。以下把它的屬性、方法與常用事件系統性地整理成一份可查閱的清單。

節點相關屬性

  • attributes:存放節點屬性的列表(唯讀)
  • childNodes:子節點列表(唯讀)
  • firstChild / lastChild:第一個/最後一個子節點(唯讀)
  • nextSibling / previousSibling:下一個/前一個兄弟節點(唯讀)
  • parentNode / ownerDocument:父節點/所屬根文件(唯讀)
  • nodeName / nodeType / nodeValue:節點名稱、類型、值
  • nodeTypedValue:節點的型別化值(可讀寫)
  • documentElement:文件根元素(可讀寫)
  • doctype / definition:文件類型節點/以 DTD 或 XML Schema 描述的節點定義(唯讀)
  • implementation:回傳 XMLDOMImplementation 物件
  • dataType:節點的資料型別
  • parsed:節點及其子節點是否已解析(唯讀)
  • prefix:命名空間前綴(唯讀)
  • preserveWhiteSpace:是否保留空白(可讀寫)
  • text / xml:節點及其後代的文字內容/XML 表示
  • url:最近載入的 XML 文件 URL(唯讀)

節點操作方法

  • appendChild / insertBefore / removeChild / replaceChild:新增、插入、移除、替換子節點
  • cloneNode:回傳節點的複本
  • createElement / createAttribute / createTextNode / createComment:建立元素、屬性、文字節點、註解節點
  • createCDATASection / createEntityReference / createDocumentFragment / createProcessingInstruction:建立 CDATA 段、實體參照、文件片段、處理指令
  • createNode:依型別、名稱與命名空間建立節點
  • getElementsByTagName:回傳指定名稱的元素集合
  • hasChildNodes:是否有子節點
  • selectNodes / selectSingleNode:以指定條件比對,回傳全部符合的節點列表/第一個符合的節點
  • transformNode / transformNodeToObject:以樣式表轉換節點及其後代/轉為物件
  • load / loadXML / save:從位置或字串載入 XML 文件、儲存 XML 至指定節點

body:文件主體

document.body                  // 文件主體,等價於 <body>...</body>
document.body.bgColor          // 背景顏色
document.body.link             // 未點擊過的連結顏色
document.body.alink            // 作用中連結(焦點在此)的顏色
document.body.vlink            // 已點擊過的連結顏色
document.body.text             // 文字顏色
document.body.innerText        // 設定 body 之間的文字
document.body.innerHTML        // 設定 body 之間的 HTML 碼
document.body.topMargin        // 上邊距
document.body.leftMargin       // 左邊距
document.body.rightMargin      // 右邊距
document.body.bottomMargin     // 下邊距
document.body.background       // 背景圖片
document.body.appendChild(oTag) // 動態產生一個 HTML 物件

常用事件

document.body.onclick = "func()"      // 單擊時觸發
document.body.onmouseover = "func()"  // 指標移入時觸發
document.body.onmouseout = "func()"   // 指標移出時觸發

location:位置子物件

document.location.hash      // # 號後的部分
document.location.host      // 域名 + 連接埠
document.location.hostname  // 域名
document.location.href      // 完整 URL
document.location.pathname  // 目錄部分
document.location.port      // 連接埠
document.location.protocol  // 網路協定(http:)
document.location.search    // ? 號後的部分

document.location.reload()       // 重新整理
document.location.reload(URL)    // 開啟新網頁
document.location.assign(URL)    // 開啟新網頁
document.location.replace(URL)   // 開啟新網頁(不留下歷史紀錄)

selection:選區子物件

document.selection

images 集合

// a) 透過集合引用
document.images          // 對應頁面上的 img 標籤
document.images.length   // img 標籤的個數
document.images[0]       // 第 1 個 img 標籤
document.images[i]       // 第 i-1 個 img 標籤

// b) 透過 name 屬性直接引用
// <img name="oImage">
document.images.oImage

// c) 引用圖片的 src 屬性
document.images.oImage.src

// d) 建立一個圖像
var oImage = new Image()
oImage.src = "1.jpg"

forms 集合

// a) 透過集合引用
document.forms                 // 對應頁面上的 form 標籤
document.forms.length          // form 標籤的個數
document.forms[0]              // 第 1 個 form
document.forms[i].length       // 第 i-1 個 form 中的控制項數量
document.forms[i].elements[j]  // 第 i-1 個 form 中第 j-1 個控制項

// b) 透過 name 屬性直接引用
// <form name="Myform"><input name="myctrl"/></form>
document.Myform.myctrl

// c) 存取表單屬性
document.forms[i].name      // 對應 form 的 name 屬性
document.forms[i].action    // 對應 form 的 action 屬性
document.forms[i].encoding  // 對應 form 的 enctype 屬性
document.forms[i].target    // 對應 form 的 target 屬性

document.forms[i].appendChild(oTag) // 動態插入一個控制項

圖層與元素存取

document.all.oDiv                        // 引用圖層 oDiv(僅 IE 支援)
document.all.oDiv.style.display = ""     // 設為可視
document.all.oDiv.style.display = "none" // 設為隱藏

document.getElementById("oDiv")          // 標準的元素引用方式
document.getElementById("oDiv").style.display = "none"

document.getElementById("ID").innerText  // 動態輸出文字
document.getElementById("ID").innerHTML  // 動態輸出 HTML
document.getElementById("ID").outerText  // 同 innerText
document.getElementById("ID").outerHTML  // 同 innerHTML

補充說明:document.all 代表文件中所有物件的集合,只有 IE 支援,因此早期常被用來判斷瀏覽器種類;現在應統一改用 getElementById、querySelector 等標準 API。

document 的事件與方法總表

類別名稱說明
事件onClick文件被點擊時觸發
onDblClick文件被雙擊時觸發
onKeyDown按下任意鍵時觸發,優先於 onKeyPress
onKeyPress按鍵輸入時觸發
onKeyUp放開按下的鍵時觸發
onMouseDown按下滑鼠鍵時觸發
onMouseUp放開滑鼠鍵時觸發
——
方法captureEvents() / releaseEvents()捕獲/釋放所有與文件相關的事件
routeEvent()透過正常處理程序傳遞所捕獲的事件
handleEvent()呼叫指定事件的事件處理器
open() / close()開啟/關閉文件流
write() / writeln()寫入內容/寫入帶換行符的字串
getSelection()回傳目前選取的文字
——
屬性alinkColor / linkColor / vlinkColor對應標籤的 alink、link、vlink 屬性
bgColor / fgColor背景顏色/文字顏色
anchors / links文件中所有 a 標籤/連結的陣列
forms / images / embeds / plugins / layers文件中各類標籤的集合陣列
cookie指定 cookie
domain服務文件的網域
lastModified文件最後修改日期
referrer來源 URL
title標題文字
URL文件的 URL
formName文件中每個 form 的實際名稱
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved