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元素節點

 

方法

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM