屬性
domain
傳回伺服器的網域名稱
referrer
傳回URI字串,指出連結此網頁的位置
title
傳回和設定title元素,即<title>
URL
傳回網頁的URL字串
開啟與寫入文件
write();
向文件寫入HTML、文字、JavaScript…
writeln();
同write()方法,但最後會包含換行符號。
open("MIME Type","replace");
清除目前的文件內容,然後使用write()或writeln()重新輸出文件的內容
MIMEType:text/html
replace可以取代歷存記錄
最後使用document.close()方法,顯示write()或writeln()輸出的文件內容
close();
Cookies
document.cookie = "name=value;expires=date;path=pname;domain=dname;secure"
name(必要)
cookie名稱
expires
cookie有效期限,為GMT格式。 例如:exDate.toGMTString();
domain
伺服器網域名稱
path
在domain下的路徑名稱
secure
如設定,表示cookie需在保密下才能傳送
domain屬性是為了分別cookie屬於那個網站所建立; path屬性可進一步區別同一網站不同網頁所建立的cookie。
getCookies = document.cookie;
取得cookie屬性的值; kname1=kvalue1;kname2=kvalue2...
我們必須自行寫Code存取Cookie的資料
Collection,集合物件
document.all
取得HTML文件中所有Tag Object
document.all(index).tagName
取得指定index的Tag Name
document.all(index).SourceIndex
取得指定index在HTML中出現的順序(IE only)
document.all.tags("P").length
使用tags()方法篩選出指定標籤的物件集合, 其中HTML標籤名稱需使用大寫字母
取得文件元素
<h2 id="header">取得文件元素</h2>
getElementById()
依HTML tag的id屬性取得指定的元素
ele = document.getElementById("header");
getElementByName()
依HTML tag的name屬性取得指定的元素
ele = document.getElementsByName("input");
getElementByTagName()
傳回一個包含某個相同標籤名稱的元素NodeList(Array)
ele = document.getElementByTagName("li"); alert( ele.length +', '+ ele[0].tagName +', '+ele[3].childNodes[0].nodeValue);
ele
tagName
tag名稱
length
同名tag有多少個
存取HTML標籤(tags)的內容和屬性
標籤內容
innerHTML
由getElementById/ByName取得文件元素後,就可以使用innerHTML來存取標籤物件內的子標籤和內容。 一般都是用來插入HTML標籤。例如在空的<div>、<span>、<p>插入所需標籤,來顯示動態效果。
標籤屬性
eleAttr = document.all.tags("P").item(0).align; MF使用:eleAttr = document.getElementById("P").align;
取得<p>屬性align的屬性值
getAttribute(attr)
取得傳入attr屬性值
ele.getAttribute("align")
setAttribute(attr,value)
將value值設定給attr屬性
ele.setAttribute("align","center")
removeAttribute(attr)
刪除傳入的attr屬性
標籤尺寸與位置
offsetLeft
標籤物件距離左方邊界的距離
offsetTop
標籤物件距離上方邊界的距離
offsetHeight
標籤物件的高
offsetWidth
標籤物件的寬
offsetParent
取得標籤物件的上一層物件
方法
scrollIntoView("true|false")
如果Broswer看不到標籤物件,自動捲動視窗顯示標籤物件
ele.scrollIntoView(true);
CSS
style
存取標籤物件的style屬性
ele.style.fontSize=12px;
CSS樣式屬性與style屬性差異
CSS
font-size
font-family
background-color
background-image
style
fontSize
fontFamily
backgroundColor
backgroundImage
style需要把中間的「-」符號刪除,然後將後面的英文字頭改為大寫。
document.all在IE使用沒有問題,但在其他Browser相容性不佳, 建議使用getElementById("id")或getElementByName("Name")來取得所需的資訊