[https://www.runoob.com/jsref/dom-obj-document.html](https://www.runoob.com/jsref/dom-obj-document.html)
[https://www.w3school.com.cn/xmldom/dom\_htmldocument.asp](https://www.w3school.com.cn/xmldom/dom_htmldocument.asp)
[https://developer.mozilla.org/zh-CN/docs/Web/API/Document](https://developer.mozilla.org/zh-CN/docs/Web/API/Document)
有window對象引入,所以它是window的一部分,可通過 window.document 屬性對其進行訪問
Document **implements** [`ParentNode`](https://developer.mozilla.org/zh-CN/docs/Web/API/ParentNode "ParentNode?混合了所有(擁有子元素的) Node 對象包含的共有方法和屬性。")、HTMLDocument、XMLDocument
>[danger]document <- HTMLDocument <- HTMLDocumentPrototype <- DocumentPrototype <- NodePrototype <- EventTargetPrototype <- Object
document.body <- HTMLBodyElementPrototype <- HTMLElementPrototype <- ElementPrototype <- NodePrototype <- EventTargetPrototype <- Object
## Document 對象屬性和方法
HTML文檔中可以使用以下屬性和方法:
| 屬性 / 方法 | 描述 |
| :-- | :-- |
|**document.**||
| [activeElement](https://www.runoob.com/jsref/prop-document-activeelement.html) | 返回當前獲取焦點元素 |
| [addEventListener()](https://www.runoob.com/jsref/met-document-addeventlistener.html) | 向文檔添加句柄 |
| [adoptNode(node)](https://www.runoob.com/jsref/met-document-adoptnode.html) | 從另外一個文檔返回 adapded 節點到當前文檔。 |
| [anchors](https://www.runoob.com/jsref/coll-doc-anchors.html) | 返回對文檔中所有 Anchor 對象的引用。 |
|all | 返回所有元素 |
| ~~applets~~ | 返回對文檔中所有 Applet 對象的引用。**注意:** HTML5 已不支持 元素。|
| [baseURI](https://www.runoob.com/jsref/prop-doc-baseuri.html) | 返回文檔的絕對基礎 URI |
| [**body**](https://www.runoob.com/jsref/prop-doc-body.html) | 返回文檔的body元素 |
| [close()](https://www.runoob.com/jsref/met-doc-close.html) | 關閉用 document.open() 方法打開的輸出流,并顯示選定的數據。 |
| [cookie](https://www.runoob.com/jsref/prop-doc-cookie.html) | 設置或返回與當前文檔有關的所有 cookie。 |
| [**createAttribute**()](https://www.runoob.com/jsref/met-document-createattribute.html) | 創建一個屬性節點 |
| [**createComment**()](https://www.runoob.com/jsref/met-document-createcomment.html) | createComment() 方法可創建注釋節點。 |
| [**createDocumentFragment**()](https://www.runoob.com/jsref/met-document-createdocumentfragment.html) | 創建空的 DocumentFragment 對象,并返回此對象。 |
| [**createElement**()](https://www.runoob.com/jsref/met-document-createelement.html) | 創建元素節點。 |
| [**createTextNode**()](https://www.runoob.com/jsref/met-document-createtextnode.html) | 創建文本節點。 |
| [doctype](https://www.runoob.com/jsref/prop-document-doctype.html) | 返回與文檔相關的文檔類型聲明 (DTD)。 |
| [**documentElement**](https://www.runoob.com/jsref/prop-document-documentelement.html) | 返回文檔的根節點html |
| [documentMode](https://www.runoob.com/jsref/prop-doc-documentmode.html) | 返回用于通過瀏覽器渲染文檔的模式 |
| [documentURI](https://www.runoob.com/jsref/prop-document-documenturi.html) | 設置或返回文檔的位置 |
| [domain](https://www.runoob.com/jsref/prop-doc-domain.html) | 返回當前文檔的域名。 |
| domConfig | **已廢棄**。返回 normalizeDocument() 被調用時所使用的配置。 |
| [embeds](https://www.runoob.com/jsref/coll-doc-embeds.html) | 返回文檔中所有嵌入的內容(embed)集合 |
| [forms](https://www.runoob.com/jsref/coll-doc-forms.html) | 返回對文檔中所有 Form 對象引用。 |
| [getElementsByClassName()](https://www.runoob.com/jsref/met-document-getelementsbyclassname.html) | 返回文檔中所有指定類名的元素集合,作為 NodeList 對象。 |
| [getElementById()](https://www.runoob.com/jsref/met-document-getelementbyid.html) | 返回對擁有指定 id 的第一個對象的引用。 |
| [getElementsByName()](https://www.runoob.com/jsref/met-doc-getelementsbyname.html) | 返回帶有指定名稱的對象集合。 |
| [getElementsByTagName()](https://www.runoob.com/jsref/met-document-getelementsbytagname.html) | 返回帶有指定標簽名的對象集合。 |
| [images](https://www.runoob.com/jsref/coll-doc-images.html) | 返回對文檔中所有 Image 對象引用。 |
| [implementation](https://www.runoob.com/jsref/prop-document-implementation.html) | 返回處理該文檔的 DOMImplementation 對象。 |
| [importNode()](https://www.runoob.com/jsref/met-document-importnode.html) | 把一個節點從另一個文檔復制到該文檔以便應用。 |
| [inputEncoding](https://www.runoob.com/jsref/prop-document-inputencoding.html) | 返回用于文檔的編碼方式(在解析時)。 |
| [lastModified](https://www.runoob.com/jsref/prop-doc-lastmodified.html) | 返回文檔被最后修改的日期和時間。 |
| [links](https://www.runoob.com/jsref/coll-doc-links.html) | 返回對文檔中所有 Area 和 Link 對象引用。 |
| [normalize()](https://www.runoob.com/jsref/met-document-normalize.html) | 刪除空文本節點,并連接相鄰節點 |
| [normalizeDocument()](https://www.runoob.com/jsref/met-document-normalizedocument.html) | 刪除空文本節點,并連接相鄰節點的 |
| [open()](https://www.runoob.com/jsref/met-doc-open.html) | 打開一個流,以收集來自任何 document.write() 或 document.writeln() 方法的輸出。 |
| [**querySelector**()](https://www.runoob.com/jsref/met-document-queryselector.html) | 返回文檔中匹配指定的CSS選擇器的第一元素 |
| [**querySelectorAll**()](https://www.runoob.com/jsref/met-document-queryselectorall.html) | document.querySelectorAll() 是 HTML5中引入的新方法,返回文檔中匹配的CSS選擇器的所有元素節點列表 |
| [readyState](https://www.runoob.com/jsref/prop-doc-readystate.html) | 返回文檔狀態 (載入中……) |
| [referrer](https://www.runoob.com/jsref/prop-doc-referrer.html) | 返回載入當前文檔的文檔的 URL。 |
| [removeEventListener()](https://www.runoob.com/jsref/met-document-removeeventlistener.html) | 移除文檔中的事件句柄(由 addEventListener() 方法添加) |
| [renameNode()](https://www.runoob.com/jsref/met-document-renamenode.html) | 重命名元素或者屬性節點。 |
| [scripts](https://www.runoob.com/jsref/coll-doc-scripts.html) | 返回頁面中所有腳本的集合。 |
| [strictErrorChecking](https://www.runoob.com/jsref/prop-document-stricterrorchecking.html) | 設置或返回是否強制進行錯誤檢查。 |
| [title](https://www.runoob.com/jsref/prop-doc-title.html) | 返回當前文檔的標題。 |
| [URL](https://www.runoob.com/jsref/prop-doc-url.html) | 返回文檔完整的URL |
| [write()](https://www.runoob.com/jsref/met-doc-write.html) | 向文檔寫 HTML 表達式 或 JavaScript 代碼。 |
| [writeln()](https://www.runoob.com/jsref/met-doc-writeln.html) | 等同于 write() 方法,不同的是在每個表達式之后寫一個換行符。 |
|[`characterSet`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/characterSet)|返回文檔正在使用的字符集|
|[`compatMode`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/compatMode)|指示文檔是否以*quirks*怪異模式或*strict*嚴格模式呈現|
|[`contentType`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/contentType)|根據當前文檔的 MIME Header,返回它的 Content-Type|
|[`fonts`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/fonts)|返回當前文檔的[`FontFaceSet`](https://developer.mozilla.org/zh-CN/docs/Web/API/FontFaceSet)接口|
|[`head`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/head)|返回當前文檔的 \<head\> 元素|
|[`hidden`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/hidden)|返回一個布爾值,表明當前頁面是否隱藏|
|[`lastStyleSheetSet`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/lastStyleSheetSet)|返回最后啟用樣式表的名字。在設置selectedStyleSheetSet 前值都為 null|
|[`plugins`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/plugins)|返回一個可用插件列表。|
|[`preferredStyleSheetSet`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/preferredStyleSheetSet)|返回由頁面作者指定的首選樣式表集|
|[`scripts`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/scripts)|返回文檔中所有的 \<script\> 元素|
|[`scrollingElement`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/scrollingElement)|返回對文檔 Element 元素的引用|
|[`selectedStyleSheetSet`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/selectedStyleSheetSet)|返回當前正使用的樣式表集。|
|[`styleSheetSets`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/styleSheetSets)|返回文檔上可用樣式表的列表|
|[`timeline`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/timeline)|返回?[`DocumentTimeline`](https://developer.mozilla.org/zh-CN/docs/Web/API/DocumentTimeline)的一個實例,該實例是在頁面加載時自動創建的|
|[`visibilityState`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document/visibilityState)|返回?`string`?表明當前文檔的可見性。可能的取值有?`visible`,`hidden`,`prerender`, and`unloaded 。`|
>[danger]注意Document是繼承與DOM節點對象的,而Document又是所有HTML文檔的根節點,很多屬性和方法在文檔中是沒有意義的列出如下
**HTML 文檔對象可以避免使用這些節點對象和屬性:**
| ?屬性 / 方法 | 避免的原因 |
| :-- | :-- |
| document.attributes | 文檔沒有該屬性 |
| document.hasAttributes() | 文檔沒有該屬性 |
| document.nextSibling | 文檔沒有下一節點 |
| document.nodeName | 這個通常是 #document |
| document.nodeType | 這個通常是 9(DOCUMENT\_NODE) |
| document.nodeValue | 文檔沒有一個節點值 |
| document.ownerDocument | 文檔沒有主文檔 |
| document.ownerElement | 文檔沒有自己的節點 |
| document.parentNode | 文檔沒有父節點 |
| document.previousSibling | 文檔沒有兄弟節點 |
| document.textContent | 文檔沒有文本節點 |
**JS獲取DOM元素的方法(8種)**
* 通過ID獲取(getElementById)
* 通過name屬性(getElementsByName)
* 通過標簽名(getElementsByTagName)
* 通過類名(getElementsByClassName)【IE9+】
* 通過選擇器獲取一個元素(querySelector)【IE8+】
* 通過選擇器獲取一組元素(querySelectorAll)【IE8+】
* 獲取html標簽(document.documentElement)
* 獲取body標簽(document.body)
**獲取元素對象**
```
document.getElementById("myBtn")
var y=x.getElementsByTagName("p");
var x=document.getElementsByClassName("intro");
document.getElementsByName("name屬性值");
document.querySelector('.animated')
```
**獲取元素的子節點**
```
/* 獲取元素節點 */
var e=document.getElementById("list");
/* 獲取該元素的子節點 */
var c=e.getElementsByTagName("li");
var c1=e.childNodes;
//返回包含標簽屬性及文本節點,標簽與標簽之間的空格與換行也會被當作文本節點(IE8及以下除外)
var c2 =e.children;//獲取所有子標簽
var c3=e.firstElementChild;//獲取當前元素第一個子標簽[IE9+]
var c4=e.firstChild;//等于c1[0]
var c5=e.lastChild;
console.log(e);
console.log(c);
console.log(c1);
console.log(c2);
console.log(c3);
console.log(c4);
更多參考節點對象
```
**offsetParent**:
獲取當前元素的開啟了position定位的最近祖先元素,如果祖先元素沒有設置定位,那么返回的是body
```
<div id="box1">
<div id="box2" style="position: relative;">
<div id="box3">
<div id="box4"></div>
</div>
</div>
</div>
<script type="text/javascript">
var box4=document.getElementById("box4");
var box=box4.offsetParent;
console.log(box);//返回box2節點對象
</script>
```
**創建元素:**
```
document.createElement("div")
```
## 改變 HTML 內容
```
document.getElementById("p1").innerHTML="新文本!";
```
## 改變 HTML 屬性
```
document.getElementById("image").src="landscape.jpg";
```
## 改變 HTML 樣式
```
document.getElementById(*id*).style.*property*\=*新樣式*
例子:
document.getElementById("p2").style.color="blue";
document.getElementById("p2").style.fontFamily="Arial";
document.getElementById("p2").style.fontSize="larger";
```
- CSS
- 達到指定寬度加載css
- 選擇器
- CSS 函數
- @media媒體查詢
- 字體
- 圖標字體
- 文本
- 光標樣式cursor
- 盒子模型
- 溢出(overflow)
- 邊框
- 不透明度opacity
- 背景(background)與漸變xx-gradient
- 輪廓(outline)與 陰影(box-shadow)
- 過渡屬性(Transition)
- 動畫屬性(Animation)
- transform變形效果旋轉,縮放,移動,傾斜等
- 顯示、隱藏與禁用
- box-sizing與resize
- 居中對齊
- css水平居中
- css垂直居中
- 文字與相鄰的元素垂直對齊
- 布局
- 高度塌陷和外邊距重疊最終解決方案
- 解決float布局時高度塌陷的最終方案after偽類元素
- 子/父元素外邊距重疊最終解決方案before偽類元素
- 傳統布局
- position布局
- position水平居中
- position垂直居中
- position水平垂直居中
- 浮動布局
- 高度塌陷和BFC
- clear
- BFC概念及觸發條件
- 表格布局
- 盒子模型布局
- 盒子水平居中布局(如margin:0 auto)
- 盒子垂直居中布局
- 相鄰元素外邊距重疊
- 行內元素的盒子模型
- 彈性伸縮布局flex
- 舊版本(IE不支持)
- 混合過渡版(僅IE10+生效)
- flex布局(新版)
- 多列布局columns
- grid網格布局(實驗性)
- 應用與總結
- 瀑布流布局
- 流式布局(響應式布局又叫百分比布局移動端一般采用)
- 用戶不能鼠標左鍵選擇文本
- 表格
- 表單
- radio
- textarea
- select
- a連接
- ul>li有序列表與ol>li無序列表
- 偽元素
- 容器寬高100%
- 瀏覽器四大內核及前綴
- 移動端開發
- 長度單位與移動端
- css_移動端開發
- rem具體解決方案
- vw具體解決方案
- 兼容性問題
- 瀏覽器默認樣式
- css預處理器
- less
- sass
- stylus
- HTML
- 標簽元素
- head的子標簽
- 文檔元素
- 文本元素
- 嵌入元素
- 分組元素
- 表格元素
- 表單元素
- input
- 標簽元素的屬性
- 全局屬性
- aria-*
- 事件on*
- data-*
- id
- class
- hidden
- style
- title
- draggable
- dropzone(實驗性)
- dir
- autocapitalize
- contenteditable
- lang
- inputmode
- accesskey
- contextmenu(移除)
- exportparts(實驗性)
- is
- itemid
- itemprop
- itemref
- itemscope
- itemtype
- XHTML遺留xml:lang和xml:base
- part(實驗性)
- slot
- spellcheck(實驗性)
- tabindex
- translate
- HTML字符實體
- 行內元素
- iframe和父頁面相互傳值,并兼容跨域問題
- a標簽嵌套解決方案
- JS
- 獲取寬度(offsetParent、clientWidth、clientHeight、offsetWidth、offsetheight、scrollWidth、scrollHeight、offsetTop、offsetLeft、scrollTop、scrollLeft)
- demo
- 全選和反選
- 定時器:
- 哪些HTML元素可以獲得焦點?
- 事件例子
- 鼠標事件
- 注冊條款
- 獲取鼠標坐標
- div跟隨鼠標移動
- 拖拽01
- 鼠標滾動事件
- 鍵盤事件
- 檢查標簽是否含有某個類
- 輪播圖
- 數組的 交集 差集 補集 并集
- 精確計算插件
- 搖獎機
- 移動端跳轉
- 基礎
- js的數據類型
- 基本類型聲明
- 引用類型聲明及用法
- 數組
- 函數
- 對象及函數原型對象
- 繼承
- js的垃圾回收機制
- javascript擴展自定義方法
- 類型轉換
- 作用域(執行上下文)及遞歸調用
- javascript事件
- 連續調用
- 排序
- 內存溢出與內存泄漏
- 系統對象
- 內置對象
- 值屬性
- Infinity
- NaN
- undefined
- globalThis
- Function 屬性
- eval()
- isFinite()
- isNaN()
- parseFloat()
- parseInt()
- decodeURI()
- decodeURIComponent()
- encodeURI()
- encodeURIComponent()
- 基本對象(Object,Function,Boolean,Symbol)
- Object
- defineProperty()
- Function
- Boolean
- Symbol
- 數字和日期對象
- Number
- Date
- BigInt
- Math
- 控制抽象化
- AsyncFunction
- Generator
- GeneratorFunction
- Promise
- Web組裝
- WebAssembly
- 結構化數據(JSON等)
- ArrayBuffer
- Atomics
- DataView
- JSON
- SharedArrayBuffer
- 使用鍵的集合對象
- Map
- Set
- WeakMap
- WeakSet
- 反射
- Reflect
- Proxy
- 可索引的集合對象(數組在這)
- Array數組
- BigInt64Array
- BigUint64Array
- Float32Array
- Float64Array
- Int16Array
- Int32Array
- Int8Array
- Uint8ClampedArray
- Uint8Array
- Uint16Array
- Uint32Array
- 國際化
- Intl
- Intl.Collator
- 文本處理(字符串與正則)
- RegExp
- String
- 錯誤對象
- Error
- InternalError
- AggregateError 實驗性
- EvalError
- RangeError
- ReferenceError
- SyntaxError
- URIError
- TypeError
- null
- TypedArray
- escape()移除但還兼容
- unescape()移除但還生效
- uneval()非標準
- arguments
- 宿主對象(DOM與Browser)
- Browser瀏覽器對象(BOM)
- Window 對象
- History 對象
- Location 對象
- Navigator 對象
- Screen 對象
- 存儲對象(localStorage與sessionStorage)
- DOM 節點對象
- EventTarget
- Node節點對象
- Document文檔節點
- HTMLDocument(HTML對象 )
- HTML 元素接口
- Element元素節點
- Attr屬性對象(與NamedNodeMap )
- DocumentType
- DocumentFragment文檔片段節點
- CharacterData
- Comment
- Text
- CDATASection
- 事件對象Event
- on-event處理器
- CustomEvent
- MouseEvent
- DragEvent
- 手勢(TouchEvent觸摸事件)
- 其他類型事件對象...
- CSSStyleDeclaration 對象
- HTMLCollection
- console對象
- MutationObserver
- 其他重要的對象(FormData與原生Ajax)
- FormData表單對象
- ajax XMLHttpRequest
- 表達式和運算符
- 算術運算符
- 賦值運算符
- 按位操作符
- 逗號操作符
- 比較操作符
- 條件運算符
- 解構賦值
- 函數表達式
- 圓括號運算符
- 邏輯運算符
- Nullish 合并操作符
- 對象初始化
- 運算符優先級
- 可選鏈
- 管道操作符 實驗性
- 屬性訪問器
- 展開語法
- 異步函數表達式
- await
- 類表達式
- delete 操作符
- function* 表達式
- in
- instanceof
- new 運算符
- new.target
- super
- this
- typeof
- void 運算符
- yield
- yield*
- 語句和聲明
- export
- default
- 控制流
- block
- break
- continue
- empty
- if...else
- switch
- throw
- try...catch
- 聲明
- const
- let
- var 描述
- 函數和類
- async function
- class
- function
- function*
- return
- 迭代
- do...while
- for
- for await...of
- for...in
- for...of
- while
- 其他
- debugger
- label
- with 移除但生效
- import
- import.meta
- 函數
- 箭頭函數
- 默認參數值
- 方法的定義
- 剩余參數
- Arguments 對象
- getter
- setter
- 類
- 類私有域
- 類元素
- 構造方法
- extends
- static
- Errors
- 更多
- 已廢棄的特性
- JavaScript 數據結構
- 詞法文法
- 屬性的可枚舉性和所有權
- 迭代協議
- 嚴格模式
- 切換到嚴格模式
- 模板字符串
- ES6(ES2015)
- Es6函數寫法
- 類class
- 導入導出模塊
- 兼容ES5
- 變量聲明
- Symbol新數據類型
- 迭代器(自定義遍歷數組)
- 生成器
- Promise異步編程
- set(集合)
- Map
- 數組新增4個方法
- 手機端事件
- bootstrap手冊
- 代碼壓縮打包
- Webpack
- 五個核心概念
- 開始
- loader
- 插件
- webpack開發環境配置
- 打包含css文件的項目
- 打包html資源
- 打包圖片資源
- 打包其他文件
- devServer(實時自動化打包)
- 總結:開發環境配置
- webpack生產環境配置
- 提取css成單獨文件
- css兼容性處理
- 壓縮css
- js語法檢查
- js兼容性處理
- js壓縮
- html壓縮
- 總結:生產環境配置
- webpack優化環境配置
- HMR( 模塊熱替換)
- source-map
- oneOf
- 緩存
- tree shaking
- code split
- demo1
- demo2
- demo3
- lazy loading
- pwa
- 多進程打包
- externals
- dll
- webpack配置詳解
- entry
- output
- module
- resolve
- dev server
- optimization
- vite
- 技能
- 前端學習路線
- 調試
- 多個版本IE瀏覽器(調試用)
- 手機端調試
- vueJS
- Element UI(一個vuejs組件)
- 瀏覽器插件開發
- 插件推薦
- 擴展文件manifest.json
- 不可視的background(常駐)頁面
- 可視頁面browser actions與page actions及八種展示方式
- 使用chrome.xxx API
- Google Chrome擴展與Web頁面/服務器之間的交互
- Google Chrome擴展中的頁面之間的數據通信
- inject-script
- chromeAPI
- pageAction
- alarms
- chrome.tabs
- chrome.runtime
- chrome.webRequest
- chrome.window
- chrome.storage
- chrome.contextMenus
- chrome.devtools
- chrome.extension
- 分類
- homepage_url 開發者或者插件主頁
- 5種類型的JS對比及消息通信
- 其它補充
- 谷歌瀏覽器截屏
- 框架及工具
- 前端UI設計網站
- 網頁中使用Unicode字符