js基礎(chǔ)之DOM中document對(duì)象的常用屬性方法詳解
-----引入
每個(gè)載入瀏覽器的 HTML 文檔都會(huì)成為 Document 對(duì)象。
Document 對(duì)象使我們可以從腳本中對(duì) HTML 頁(yè)面中的所有元素進(jìn)行訪問(wèn)。
屬性
1 document.anchors 返回對(duì)文檔中所有 Anchor 對(duì)象的引用。還有document.links/document.forms/document.images等
2 document.URL 返回當(dāng)前文檔的url
3 document.title 返回當(dāng)前文檔的標(biāo)題
4 document.body 返回body元素節(jié)點(diǎn)
方法
1 document.close() 關(guān)閉用 document.open() 方法打開(kāi)的輸出流,并顯示選定的數(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é)點(diǎn)。
<!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é)點(diǎn)。
<!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é)點(diǎn)。
<!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 對(duì)象集合。所謂NodeList對(duì)象集合,是一個(gè)類似于數(shù)組的數(shù)據(jù)格式,僅僅提供了length屬性,像數(shù)組中的push pop方法等都未提供。
6 document.getElementById() 返回對(duì)擁有指定 id 的第一個(gè)對(duì)象的引用。
7 document.getElementsByName() 返回帶有指定名稱的對(duì)象集合。
8 document.getElementsByTagName() 返回帶有指定標(biāo)簽名的對(duì)象集合。
9 document.write() 向文檔寫(xiě) HTML 表達(dá)式 或 JavaScript 代碼。注意:當(dāng)html文檔加載完后再使用write方法,會(huì)導(dǎo)致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ī)?lái)的js基礎(chǔ)之DOM中document對(duì)象的常用屬性方法詳解全部?jī)?nèi)容了,希望大家多多支持腳本之家~
- JavaScript——DOM操作——Window.document對(duì)象詳解
- JavaScript中document對(duì)象使用詳解
- JavaScript基礎(chǔ)語(yǔ)法、dom操作樹(shù)及document對(duì)象
- 淺析JS中document對(duì)象的一些重要屬性
- js禁止document element對(duì)象選中文本實(shí)現(xiàn)代碼
- JavaScript的document對(duì)象和window對(duì)象詳解
- javascript代碼在ie8里報(bào)錯(cuò) document.getElementById(...) 為空或不是對(duì)象的解決方法
- JavaScript document 對(duì)象常用方法
相關(guān)文章
WebApi+Bootstrap+KnockoutJs打造單頁(yè)面程序
這篇文章主要介紹了WebApi+Bootstrap+KnockoutJs打造單頁(yè)面程序的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
學(xué)習(xí)javascript文件加載優(yōu)化
這篇文章主要為大家詳細(xì)介紹了javascript文件加載優(yōu)化,三種方式實(shí)現(xiàn)js文件加載優(yōu)化,感興趣的小伙伴們可以參考一下2016-02-02
詳解uniapp如何處理圖片加載過(guò)程中的錯(cuò)誤
這篇文章給大家詳細(xì)介紹了uniapp如何處理圖片加載過(guò)程中的錯(cuò)誤,文章通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-10-10

