1. 程式人生 > >JS的Document屬性和方法

JS的Document屬性和方法

document 當前網頁(文件)物件。 當前網頁裡所有元素(物件)全部都在document中。每個載入瀏覽器的 HTML 文件都會成為 Document 物件。

Document 物件使我們可以從指令碼中對 HTML 頁面中的所有元素進行訪問。

document.title //設定文件標題等價於HTML的title標籤
document.bgColor //設定頁面背景色
document.fgColor //設定前景色(文字顏色)
document.linkColor //未點選過的連結顏色
document.alinkColor //啟用連結(焦點在此連結上)的顏色
document.vlinkColor //已點選過的連結顏色
document.URL //設定URL屬性從而在同一視窗開啟另一網頁
document.fileCreatedDate //檔案建立日期,只讀屬性
document.fileModifiedDate //檔案修改日期,只讀屬性
document.charset //設定字符集 簡體中文:gb2312
document.fileSize //檔案大小,只讀屬性
document.cookie //設定和讀出cookie

———————————————————————

常用物件方法

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 // 域名+埠號//好像返回的是主機名localhost,沒有返回埠號
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

例如:

<div>請選中這裡的部分文字。</div>
<div><input type="button" value="請選中部分文字,然後點選這裡執行 empty" onclick="javascript:Foo();" /></div>
<script type="text/javascript" language="javascript">
<!--
function Foo()
{
    document.selection.empty();
}
-->
</script>

selection的createRange方法

document.selection.createRange() 根據當前文字選擇返回 TextRange 物件,或根據控制元件選擇返回 ControlRange 物件。

配合 execCommand,在 HTML 編輯器中很有用,比如:文字加粗、斜體、複製、貼上、建立超連結等。

這些好像都是隻有在IE下才能實現。。

———————————————————————

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 //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