JS Document 對象內容集合

本文主要內容轉載自 平凡的世界 Document對象內容集合 一文。

為了搞清楚 document.forms[0].elements 到底是什麼,找到了這篇整理了關於 document 的各項功用的文章,內容實用度滿檔阿!


以下為轉載內容:

———————————————————————
注:頁面上元素name屬性和JavaScript引用的名稱必須一致包括大小寫
否則會提示你一個錯誤資訊 “引用的元素為空或者不是物件\\\\\”
———————————————————————
物件屬性
document.title //設置文檔標題等價於HTML的title標籤
document.bgColor //設置頁面背景色
document.fgColor //設置前景色(文本顏色)
document.linkColor //未點擊過的鏈結顏色
document.alinkColor //啟動鏈結(焦點在此鏈結上)的顏色
document.vlinkColor //已點擊過的鏈結顏色
document.URL //設置URL屬性從而在同一視窗打開另一網頁
document.fileCreatedDate //檔建立日期,唯讀屬性
document.fileModifiedDate //檔修改日期,唯讀屬性
document.fileSize //檔大小,唯讀屬性
document.cookie //設置和讀出cookie
document.charset //設置字元集 簡體中文:gb2312
———————————————————————
常用物件方法
document.write() //動態向頁面寫入內容
document.createElement(Tag) //創建一個html標籤物件
document.getElementById(ID) //獲得指定ID值的對象
document.getElementsByName(Name) //獲得指定Name值的對象
document.body.appendChild(oTag)
———————————————————————
body-主體子物件
document.body //指定文檔主體的開始和結束等價於body>/body>
document.body.bgColor //設置或獲取物件後面的背景顏色
document.body.link //未點擊過的鏈結顏色
document.body.alink //啟動鏈結(焦點在此鏈結上)的顏色
document.body.vlink //已點擊過的鏈結顏色
document.body.text //文本色
document.body.innerText //設置body>…/body>之間的文本
document.body.innerHTML //設置body>…/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 // ?號後的部分
documeny.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)通過nane屬性直接引用
img name=”oImage”
document.images.oImage //document.images.name屬性
c)引用圖片的src屬性
document.images.oImage.src //document.images.name屬性.src
d)創建一個圖像
var oImage
oImage = new Image()
document.images.oImage.src=”1.jpg”
同時在頁面上建立一個img /標籤與之對應就可以顯示
———————————————————————-
forms集合(頁面中的表單)
a)通過集合引用
document.forms //對應頁面上的form標籤
document.forms.length //對應頁面上/formform標籤的個數
document.forms[0] //第1個/formform標籤
document.forms[i] //第i-1個/formform標籤
document.forms[i].length //第i-1個/formform中的控制項數
document.forms[i].elements[j] //第i-1個/formform中第j-1個控制項
b)通過標籤name屬性直接引用
/formform name=”Myform”>input name=”myctrl”/>/form
document.Myform.myctrl //document.表單名.控制項名
c)訪問表單的屬性
document.forms[i].name //對應form name>屬性
document.forms[i].action //對應/formform action>屬性
document.forms[i].encoding //對應/formform enctype>屬性
document.forms[i].target //對應/formform target>屬性
document.forms[i].appendChild(oTag) //動態插入一個控制項
document.all.oDiv //引用圖層oDiv
document.all.oDiv.style.display=”" //圖層設置為可視
document.all.oDiv.style.display=”none” //圖層設置為隱藏
document.getElementId(”oDiv”) //通過getElementId引用對象
document.getElementId(”oDiv”).style=”"
document.getElementId(”oDiv”).display=”none”
/*document.all表示document中所有物件的集合
只有ie支援此屬性,因此也用來判斷瀏覽器的種類*/
圖層物件的4個屬性
document.getElementById(”ID”).innerText //動態輸出文本
document.getElementById(”ID”).innerHTML //動態輸出HTML
document.getElementById(”ID”).outerText //同innerText
document.getElementById(”ID”).outerHTML //同innerHTML

以上資料來源:Document對象內容集合
 

© Copyright Tech Minute . All Rights Reserved.

Designed by TemplateWorld and sponsored by SmashingMagazine

Blogger Template created by Deluxe Templates