js基礎之DOM中document對象的常用屬性方法詳解
-----引入
每個載入瀏覽器的 HTML 文檔都會成為 Document 對象。
Document 對象使我們可以從腳本中對 HTML 頁面中的所有元素進行訪問。
屬性
1 document.anchors 返回對文檔中所有 Anchor 對象的引用。還有document.links/document.forms/document.images等
2 document.URL 返回當前文檔的url
3 document.title 返回當前文檔的標題
4 document.body 返回body元素節(jié)點
方法
1 document.close() 關(guān)閉用 document.open() 方法打開的輸出流,并顯示選定的數(shù)據(jù)。
<!DOCTYPE html> <html> <head> <script> function createDoc() { var w=window.open(); w.document.open(); w.document.write("<h1>Hello World!</h1>"); w.document.close(); } </script> </head> <body> <input type="button" value="New document in a new window" onclick="createDoc()"> </body> </html>
2 document.createElement() 創(chuàng)建元素節(jié)點。
<!DOCTYPE html> <html lang="en"> <!DOCTYPE html> <head> </head> <body> <p>hello world</p> <script> var a = document.createElement('hr'); document.body.appendChild(a) </script> </body> </html>
3 document.createAttribute() 創(chuàng)建屬性節(jié)點。
<!DOCTYPE html> <html> <body> <p id="demo">Click the button to make a BUTTON element.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var btn=document.createElement("BUTTON"); document.body.appendChild(btn); }; </script> </body> </html>
4 document.createTextNode() 創(chuàng)建文本節(jié)點。
<!DOCTYPE html> <html lang="en"> <!DOCTYPE html> <head> </head> <body> <p>hello world</p> <script> var a = document.createTextNode('hahahah'); document.body.appendChild(a) </script> </body> </html>
5 document. getElementsByClassName() 返回文檔中所有指定類名的元素集合,作為 NodeList 對象集合。所謂NodeList對象集合,是一個類似于數(shù)組的數(shù)據(jù)格式,僅僅提供了length屬性,像數(shù)組中的push pop方法等都未提供。
6 document.getElementById() 返回對擁有指定 id 的第一個對象的引用。
7 document.getElementsByName() 返回帶有指定名稱的對象集合。
8 document.getElementsByTagName() 返回帶有指定標簽名的對象集合。
9 document.write() 向文檔寫 HTML 表達式 或 JavaScript 代碼。注意:當html文檔加載完后再使用write方法,會導致write內(nèi)容覆蓋掉原本的html文檔。
<!DOCTYPE html> <html lang="en"> <!DOCTYPE html> <head> </head> <body> <p>hello world</p> <script> document.write('hahahh') </script> </body> </html>
以上就是小編為大家?guī)淼膉s基礎之DOM中document對象的常用屬性方法詳解全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
WebApi+Bootstrap+KnockoutJs打造單頁面程序
這篇文章主要介紹了WebApi+Bootstrap+KnockoutJs打造單頁面程序的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05