在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ 文檔樹
書單
JavaScript 動畫
HTML
CSS Reset
屬性操作
DOM 事件
閉包
Photoshop
Atom 文本編輯器
表單操作
布局解決方案
類型系統(tǒng)
開發(fā)實踐
數據通信
變量作用域
BOM
JavaScript 程序設計
前端工程師概述
CSS
響應式布局
表達式與運算符
基本語法
JavaScript 介紹
版本控制
布局
調試器
背景
圖片保存
多媒體
文檔樹
列表操作
Sublime 編輯器
盒模型
常見布局樣例
類型識別
變形
數據存儲
選擇器
頁面架構
開發(fā)及調試工具
頁面模塊化
節(jié)點操作
測量及取色
瀏覽器兼容
HTML 簡介
內置對象
實體字符
產品前端架構
協(xié)作流程
切圖
工具, 面板, 視圖
正則表達式
動畫
語句
面向對象
HTML 語法
HTML 標簽
技術選擇
樣式操作
圖片優(yōu)化與合并
語法
DOM 編程藝術
Canvas
接口設計
頁面優(yōu)化
文本

文檔樹

文檔樹

Document Object Model (DOM) 為文檔對象模型, 它使用對象的表示方式來表示對應的文檔結構及其中的內容。

下面為一個樣例 p 元素在文檔中的對象所包含的所有屬性。

<p id="target">Hello, World!</p>
p#targetaccessKey: ""
align: ""
attributes: Named
NodeMapbaseURI: ""
childElementCount: 0
childNodes: NodeList[1]
children: HTMLCollection[0]
classList: DOMTokenList[0]
className: ""
clientHeight: 0
clientLeft: 0
clientTop: 0
clientWidth: 0
contentEditable: "inherit"
dataset: DOM
StringMapdir: ""
draggable: false
firstChild: text
firstElementChild: null
hidden: false
id: "target"
innerHTML: "Hello, World!"
innerText: "Hello, World!"
isContentEditable: false
lang: ""
lastChild: text
lastElementChild: null
localName: "p"
namespaceURI: "http://www.w3.org/1999/xhtml"
nextElementSibling: null
nextSibling: null
nodeName: "P"
nodeType: 1
nodeValue: null
offsetHeight: 0
offsetLeft: 0
offsetParent: null
offsetTop: 0
offsetWidth: 0
onabort: null
onautocomplete: null
onautocompleteerror: null
onbeforecopy: null
onbeforecut: null
onbeforepaste: null
onblur: null
oncancel: null
oncanplay: null
oncanplaythrough: null
onchange: null
onclick: null
onclose: null
oncontextmenu: null
oncopy: null
oncuechange: null
oncut: null
ondblclick: null
ondrag: null
ondragend: null
ondragenter: null
ondragleave: null
ondragover: null
ondragstart: null
ondrop: null
ondurationchange: null
onemptied: null
onended: null
onerror: null
onfocus: null
oninput: null
oninvalid: null
onkeydown: null
onkeypress: null
onkeyup: null
onload: null
onloadeddata: null
onloadedmetadata: null
onloadstart: null
onmousedown: null
onmouseenter: null
onmouseleave: null
onmousemove: null
onmouseout: null
onmouseover: null
onmouseup: null
onmousewheel: null
onpaste: null
onpause: null
onplay: null
onplaying: null
onprogress: null
onratechange: null
onreset: null
onresize: null
onscroll: null
onsearch: null
onseeked: null
onseeking: null
onselect: null
onselectstart: null
onshow: null
onstalled: null
onsubmit: null
onsuspend: null
ontimeupdate: null
ontoggle: null
onvolumechange: null
onwaiting: null
onwebkitfullscreenchange: null
onwebkitfullscreenerror: null
onwheel: null
outerHTML: "<p id="target">Hello, World!</p>"
outerText: "Hello, World!"
ownerDocument: document
parentElement: null
parentNode: null
prefix: null
previousElementSibling: null
previousSibling: null
scrollHeight: 0
scrollLeft: 0
scrollTop: 0
scrollWidth: 0
shadowRoot: null
spellcheck: true
style: CSSStyle
DeclarationtabIndex: -1
tagName: "P"
textContent: "Hello, World!"
title: ""
translate: true
webkitdropzone: ""
__proto__: HTMLParagraphElement

通過使用 DOM 提供的 API (Application Program Interface) 可以動態(tài)的修改節(jié)點(node),也就是對 DOM 樹的直接操作。 瀏覽器中通過使用 JavaScript 來實現對于 DOM 樹的改動。

DOM 包含

  • DOM Core
  • DOM HTML
  • DOM Style
  • DOM Event

HTML 轉換 DOM 樹

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>My title</title>
  </head>
  <body>
    <a href="">My Link</a>
    <h1>My header</h1>
  </body>
</html>

http://wiki.jikexueyuan.com/project/fend_note/images/D/dom-tree.gif" alt="" />

節(jié)點遍歷

在元素節(jié)點中提取自己所需的節(jié)點,并予以操作。

// Document.getElementsByTagName()
// 更具標簽名找到目標節(jié)點的集合,此例中為 <h1>My header</h1>
var node = document.getElementsByTagName('h1')[0];

// Node.parentNode;
// 獲得目標節(jié)點的父節(jié)點,此例中為 body 元素
node.parentNode;

// Node.firstChild
// 獲得目標節(jié)點的第一個子節(jié)點,此例中為 "My header"
node.firstChild;

// Node.lastChild
// 獲得目標節(jié)點的最后一個子節(jié)點,此例中為 "My header"
node.lastChild;

// Node.previousSibling;
// 獲得目標節(jié)點的前一個相鄰節(jié)點
node.previousSibling;

// Node.nextSibling;
// 獲得目標節(jié)點的下一個相鄰節(jié)點
node.nextSibling;

節(jié)點類型

常用節(jié)點類型

  • ELEMENT_NODE 可使用 Document.createElement('elementName'); 創(chuàng)建
  • TEXT_NODE 可使用 Document.createTextNode('Text Value'); 創(chuàng)建

不常用節(jié)點類型

  • COMMENT_NODE
  • DOCUMENT_TYPE_NODE

不同節(jié)點對應的NodeType類型

此值可以通過 Node.nodeType 來獲取。

節(jié)點編號 節(jié)點名稱
1 Element
2 Attribute
3 Text
4 CDATA Section
5 Entity Reference
6 Entity
7 Processing Instrucion
8 Comment
9 Document
10 Document Type
11 Document Fragment
12 Notation

NOTE:此處需要清楚節(jié)點元素的區(qū)別。我們平常說的元素 其實指的是節(jié)點中得元素節(jié)點,所以說節(jié)點包含元素,節(jié)點還包括文本節(jié)點、實體節(jié)點等。

元素遍歷

元素節(jié)點符合 HTML DOM 樹規(guī)則,所以它與 DOM 中存在的節(jié)點相似。

<p>
  Hello,
    <em>Xinyang</em>!
  回到
    <a >
      主頁
    </a>
  。
</p>
// 在選取元素節(jié)點后

p.firstElementChild;       // <em>Xinyang</em>
p.lastElementChild;        // <a >主頁</a>

em.nextElementSibling;     // <a >主頁</a>
em.previousElementSibling; // "Hello,"
上一篇:屬性操作下一篇:語句