標籤:des style http java 使用 os io 資料
Document 對象
每個載入瀏覽器的 HTML 文檔都會成為 Document 對象。
Document 對象使我們可以從指令碼中對 HTML 頁面中的所有元素進行訪問。
提示:Document 對象是 Window 對象的一部分,可通過 window.document 屬性對其進行訪問。
Document 對象集合
| 集合 |
描述 |
| all[] |
提供對文檔中所有 HTML 元素的訪問。 |
| anchors[] |
返回對文檔中所有 Anchor 對象的引用。 |
| applets |
返回對文檔中所有 Applet 對象的引用。 |
| forms[] |
返回對文檔中所有 Form 對象引用。 |
| images[] |
返回對文檔中所有 Image 對象引用。 |
| links[] |
返回對文檔中所有 Area 和 Link 對象引用。 |
Document 對象屬性
| 屬性 |
描述 |
| body |
提供對 <body> 元素的直接存取。
對於定義了框架組的文檔,該屬性引用最外層的 <frameset>。 |
| cookie |
設定或返回與當前文檔有關的所有 cookie。 |
| domain |
返回當前文檔的網域名稱。 |
| lastModified |
返迴文檔被最後修改的日期和時間。 |
| referrer |
返回載入當前文檔的文檔的 URL。 |
| title |
返回當前文檔的標題。 |
| URL |
返回當前文檔的 URL。 |
Document 對象方法
| 方法 |
描述 |
| close() |
關閉用 document.open() 方法開啟的輸出資料流,並顯示選定的資料。 |
| getElementById() |
返回對擁有指定 id 的第一個對象的引用。 |
| getElementsByName() |
返回帶有指定名稱的對象集合。 |
| getElementsByTagName() |
返回帶有指定標籤名的對象集合。 |
| open() |
開啟一個流,以收集來自任何 document.write() 或 document.writeln() 方法的輸出。 |
| write() |
向文檔寫 HTML 運算式 或 JavaScript 代碼。 |
| writeln() |
等同於 write() 方法,不同的是在每個運算式之後寫一個分行符號。 |
一:Dom模型的全稱--->Document Object Model
---->文件物件模型
二WEB頁面的html文檔,document就是根節點。其他的子物件就是子節點
三節點類型 (1)元素節點 例子:<input>
(2)文本節點 例子:尚曉飛 文本節點
(3)屬性節點 例子:<input type="shang"/> shang 就是屬性節點
四擷取元素節點的方法
【1】直接擷取節點:
(1)擷取指定的id屬性指定值的節點-->唯一
var name=document.getElementById("uname");
(2)擷取指定的name屬性值的節點--->返回的是一組
var ad=document.getElementsByName("unameName");
(3)擷取指定元素名的節點--->返回的是一組
var aaa=document.getElementsByTagName("input");
【2】間接擷取節點
(1)通過父節點,擷取子節點。
//擷取父節點
var father=document.getElementById("showDiv");
//擷取該父節點的子節點node集合
var sonarray=father.childNodes;
alert(array[2].nodeValue);
(2)通過位元組點,擷取父節點
//通過子節點,擷取父節點
var son=document.getElementById("a");
var fathera=son.parentNode;
alert(fathera.id);
(3)通過兄弟節點,擷取節點
//兄弟節點
var borther2=document.getElementById("realname");
var borther1=borther2.previousSibling;//上一個兄弟節點
var borther3=borther2.nextSibling;//下一個兄弟節點
while(borther1.nodeType!=1){
borther1=borther1.previousSibling;
}
alert(borther1.value);
注意:節點的三個屬性 ---->nodeName
----->nodeValue
----->nodeType
四:處理屬性節點
(1)元素節點.屬性----->擷取屬性值,也可以為屬性重新賦值
(2)getAttribute(key)--->擷取key屬性的值
setAttribute(key,value)--->為該節點設定屬性,及屬性的值
五:處理文本節點
(1)擷取一個節點內的文本,一般使用innerHtml
(2)innerHtml不局限操作一個節點,而是可以使用Html片段直接填充頁面,或直接擷取Html片段。大大提高開發的靈活性
例子: function innerhtml(){
alert(document.getElementById("showDiv").innerHTML);
}
六 改變文檔的階層
(1)建立元素節點document.creatElement
var input=document.createElement("input");
input.type="text";
input.id="123";
input.value="天下無雙";
(2)通過父節點,添加到父節點裡。parentNode.appendChild
var father=document.getElementById("showDiv");
father.appendChild(input);
(3)插入節點 parentNode.insertBefore(newnode,beforenode)
var ab=document.getElementById("a");
father.insertBefore(input,ab);
(4)覆蓋節點 parentNode.replaceChild(newnode,oldnode)
舊節點必須存在,否則發生異常
father.replaceChild(input,ab);
(5)移除某個節點parentNode.removeChild(childNode)
father.removeChild(ab)