js基礎之DOM中document對象的常用屬性方法

來源:互聯網
上載者:User

標籤:

-----引入

每個載入瀏覽器的 HTML 文檔都會成為 Document 對象。

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

 

屬性

1  document.anchors  返回對文檔中所有 Anchor 對象的引用。還有document.links/document.forms/document.images等

2  document.URL       返回當前文檔的url

3  document.title       返回當前文檔的標題

 

方法

1  document.close()     關閉用 document.open() 方法開啟的輸出資料流,並顯示選定的資料。

<!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>
View Code

2  document.createElement()     建立元素節點。

<!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>
View Code

3  document.createAttribute()   建立屬性節點。

<!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>
View Code

4  document.createTextNode()  建立文本節點。

<!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>
View Code

5  document. getElementsByClassName()   返迴文檔中所有指定類名的元素集合,作為 NodeList 對象集合。所謂NodeList對象集合,是一個類似於數組的資料格式,僅僅提供了length屬性,像數組中的push  pop方法等都未提供。

6  document.getElementById() 返回對擁有指定 id 的第一個對象的引用。

7  document.getElementsByName() 返回帶有指定名稱的對象集合。

8  document.getElementsByTagName() 返回帶有指定標籤名的對象集合。

9  document.write() 向文檔寫 HTML 運算式 或 JavaScript 代碼。注意:當html文檔載入完後再使用write方法,會導致write內容覆蓋掉原本的html文檔。

<!DOCTYPE html><html lang="en"><!DOCTYPE html><head></head><body>    <p>hello world</p><script>    document.write(‘hahahh‘)</script></body></html>
View Code

 

js基礎之DOM中document對象的常用屬性方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.