DOM 即文档对象模型,是操做 HTML/XML 文档的一套方法。经过 DOM 操做节点,使页面发生改变,是动态文档技术的核心内容html
本地对象 Native Objectnode
Object Function String Number Boolean
Error EvalError SyntaxError TypeError RageError ReferenceError URIError
Date RegExpweb
内置对象 Built-in Objectapi
Global: 一类对象的统称,是虚拟的,表明全局
任何方法和属性都在对象之下
如 isNaN()、parseInt、Number()、decodeURL() 等都是 Global 下的方法
如 Math、Infinity、NaN、undefined 等都是 Global 下的属性
本地对象、内置对象是 ECMAScript 的内部对象数组
宿主对象 Host Object浏览器
执行 JS 的环境提供的对象,即浏览器对象
window 对象 -> BOM,不一样浏览器间没有固定规范
document 对象 -> DOM,听从 w3c 规范
document 是 window 下的对象,即 BOM 包含 DOMapp
元素节点、属性节点、文本节点、注释节点、文档节点等性能
document.__proto__ -> HTMLDocument.prototype
HTMLDocument.prototype.__proto__ -> Document.prototypeui
document.getElementById(); // 返回对应的 HTMLElement
// 对于 getElementById(),IE8 及如下不区分大小写而且能够匹配 namethis
document.getElementsByClassName(); // 返回元素集合 HTMLCollection
// 兼容 IE8 及以上
document.getElementsByTagName(); // 返回元素集合 HTMLCollection
// 兼容 IE8 及以上
document.getElementsByName(); // 返回元素集合 HTMLCollection
// 经常使用于表单元素
// 兼容 IE6 及以上,是 HTML5 新加入的 web api,但早就存在了
document.querySelector(); // 返回选择器选中的第一个节点对象 Node
document.querySelectorAll(); // 返回选择器选中的节点列表 NodeList
// querySelector、querySelectorAll 性能低
// 使用 querySelectorAll 返回节点列表,删除节点后,节点列表不变,不具备实时性
获取父节点
document.getElementsByTagName("html")[0].parentNode -> document
// html 标签元素的 parentNode 是 document
// 最高级节点是 document,document 的父节点是 null
获取子节点集合
document.getElementById("box").childNodes
// 包括元素节点、文本节点、注释节点在内
获取第一个或最后一个子节点
document.getElementById("box").firstChild // 第一个子节点
document.getElementById("box").lastChild // 第二个子节点
获取上一个或下一个兄弟节点
document.getElementById("box").nextSibling // 上一个兄弟节点
document.getElementById("box").previousSibling // 下一个兄弟节点
获取属性节点
document.getElementById("box").getAttributeNode("id") // 获取属性节点 id
获取父元素节点,兼容 IE9 及以上
document.getElementsByTagName("html")[0].parentElement -> null
// html 标签元素的 parentElement 是 null
获取子元素集合,兼容 IE8 及以上
document.getElementById("box").children // 只包含元素节点
document.getElementById("box").chilElementCount // children.length
获取第或最后一个子元素节点,兼容 IE9 及以上
document.getElementById("box").firstElementChild // 第一个子元素节点
document.getElementById("box").lastElementChild // 第二个子元素节点
获取下一个或上一个兄弟元素节点,兼容 IE9 及以上
document.getElementById("box").nextElementSibling // 上一个兄弟元素节点
document.getElementById("box").previousElementSibling // 下一个兄弟元素节点
返回节点名称、只读
元素节点,元素名称大写
document.getElementById("box").nodeName // DIV
// 文本节点 -> #text
// 注释节点 -> #comment
document.nodeName // #document
// 文档节点 -> #document
返回节点内容,可读写
属性节点、文本节点、注释节点可用
document.getElementById("box").getAttributeNode('id').nodeValue // box
document.getElementById("box").getAttributeNode('id').value // 效果相同
nodeType
返回节点类型对应数字,只读
元素节点 1
属性节点 2
文本节点 3
注释节点 8
document 9
DocumentFragment 11
经过 nodeType 封装 childNodes,筛选出元素节点
function childElementArray(node) {
var arr = \[\], nodes = node.childNodes; for (var i = 0; i < nodes.length; i++) { var item = nodes\[i\]; if (item.nodeType === 1) { arr.push(item); } } return arr;
}
返回元素节点的属性节点组成的类数组
document.getElementById("box").attributes[0].nodeValue // 获取第一个属性节点的 nodeValue
返回是否有子节点的布尔值
graph TB A(Node) –> B(Document) A –> C(Element) A –> D(CharacterData) A –> E(Attributes) B –> F(HTMLDocument) D –> G(Text) D –> H(Comment) C –> I(HTMLElement) I –> J(HTMLHtmlElement) I –> K(HTMLHeadElement) I –> L(HTMLDivElement) I –> M(HTML…Element)
getElementById、getElementsByName 只在 Document 的原型上,只有 document 可使用
querySelector、querySelectorAll、getElementsByClassName、getElementsByTagName 在 Document 和 Element 的原型上都有,即 document 和元素节点对象均可以使用
var box = document.getElementById("box");
box.getElementById("list"); // 报错
document.bady 、document.head、document.title 继承自 HTMLDocoment 的原型
document.documentElement 继承自 Document 的原型
document.bady; // body 标签元素
document.head; // head 标签元素
document.title; // title 元素内文字节点
document.documentElement; // html 标签元素
建立元素节点
var div = document.createElement("div"); // 传入标签名
建立文本节点
建立注释节点
建立一个文档碎片,向其中添加节点,再将文档碎片插入文档中,能够提升性能
appendChild()
在节点内部的最后添加子节点,而且具备剪切节点功能
var div = document.createElement("div"),
text = document.creaetTextNode("文本");
div.appendChild(text);
document.body.appendChild(div);
insertBefore(a, b)
在节点内部 a 节点以前插入 b 节点
元素节点没有 insertAfter 方法,能够进行封装
Element.prototype.insertAfter = function (target, origin) {
var after = origin.nextElementSibling; if (after) { this.insertBefore(target, after); } else this.appendChild(target);
}
// 兼容 IE9 及以上
从文档中删除子节点,但内存中依然保留空间占用
节点调用,删除自身,能够释放内存
用 a 节点替换子节点 b
能够取值,能够赋值,而且赋值能够解析 HTML 代码
能够取值,能够赋值,而且将赋值解析成文本节点,标签符号会被转码成字符实体
firefox 老版本不支持,可以使用功能相同的 textContent
设置、获取属性
var div = document.createElement("div");
div.setAttribute('id', 'box');
div.getAttribute('id'); // 返回 box
HTML5 中以命名 data-
开头的属性,能够经过 dataset 方法访问
// <p data-name="Jett" data-age="22"></p>document.getElementsByTagName("p")[0].dateset // {name: 'Jett', age: '22'}// 兼容 IE9 及以上