相比DOM1, DOM2和DOM3引入了更多的交互能力,支持了更高级的XML特性javascript
// 下面的代码判断浏览器是否支持这些DOM模块 document.implementation.hasFeature('Core', 2.0) document.implementation.hasFeature('Core', 3.0) document.implementation.hasFeature('HTML', 2.0) document.implementation.hasFeature('Views', 2.0) document.implementation.hasFeature('XML', 2.0)
引入命名空间的做用:不一样XML文档的元素能够混在一块儿(好比XHTML和SVG语言混合的文档)没必要担忧命名冲突css
如何定义命名空间:使用xmlns特性html
下面是一个命名空间为http://www.w3.org/1999/xhtml的xhtml页面例子java
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Example XHTML page</title> </head> <body> Hello World! </body> </html>
若是想为XML命名空间建立前缀,可使用 xmlns:prefixnode
下面的例子使用了xhtml的前缀,固然特性也可使用命名空间来限制编程
<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml"> <xhtml:head> <xhtml:title>Example XHTML page</xhtml:title> </xhtml:head> <xhtml:body xhtml:class="home"> Hello World! </xhtml:body> </xhtml:html>
在DOM2级中,Node类型包含下列特定于命名空间的属性浏览器
当使用了命名空间前缀时,nodeName = prefix : localName框架
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Example XHTML page</title> </head> <body> <s:svg xmlns:s="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100" style="width:100%, height:100%"> <s:rect x="0" y="0" width="100" height="100" style="fill:red" /> </s:svg> </body> </html>
对于<s:svg>元素而言,它的localName是"svg", tagName是"s:svg", namespaceURI是“http://www.w3.org/2000/svg”, prefix是“s”svg
包含了与命名空间有关的方法,方法名的后面两个字母为NS, 意为namespacespa
DOM2级中新增属性contentDocument:指向表示框架内容的文档对象
let iframe = document.getElementById('myIframe') let iframeDoc = iframe.contentDocument // IE8之前的版本中无效 // 兼容方法,可使用contentWindow let iframeDoc = iframe.contentDocument || iframe.contentWindow.document
HTML中定义样式有三种方式:
// 判断浏览器是否支持DOM2级定义的CSS var supportsDOM2CSS = document.implementation.hasFeature('CSS', 2.0) var supportsDOM2CSS2 = document.implementation.hasFeature('CSS2', 2.0)
2.2.1 访问元素的样式
经过Javascript中的style属性,能够访问HTML元素的style特性,包含经过style特性指定的全部样式信息,但不包含外部样式表或嵌入式样式表经层叠而来的样式
"float"比较特殊,由于"float"是Javascript中的保留字,因此“DOM2级样式”规范规定属性名为"cssFloat", IE支持的是"styleFloat"
1) DOM样式属性和方法
举个例子:
元素样式覆盖了外部样式表中的样式,Javascript的style对象只返回元素的样式对象
HTML:
<div id="myDiv" style="background-color: green;width:50px;">myDiv</div>
CSS:
#myDiv { width: 100px; height: 100px; background-color: red; }
JS:
let i, len, prop, value; let myDiv = document.querySelector("#myDiv") // cssText let cssText = myDiv.style.cssText console.log(cssText) // background-color: green; width: 50px; // length + item() for(i = 0, len = myDiv.style.length; i < len; i++) { console.log(myDiv.style[i]) // 或者 myDiv.style.item(i) } // background-color // width // getPropertyValue for(i = 0, len = myDiv.style.length; i < len; i++) { prop = myDiv.style.item(i) value = myDiv.style.getPropertyValue(prop) console.log(prop + ':' + value) // background-color:green // width:50px }
2) 计算的样式
document.defalultView.getComputedStyle(element, pseudo)
<style> #myDiv { background-color: blue; width: 100px; height: 200px; } </style> <div id="myDiv" style="background-color:red;border:1px solid black;">myDiv</div>
let myDiv = document.getElementById('myDiv') let computedStyle = document.defaultView.getComputedStyle(myDiv, null) console.log(computedStyle.backgroundColor) // rgb(255, 0, 0) console.log(computedStyle.width) // 100px console.log(computedStyle.height) // 200px console.log(computedStyle.border) // 1px solid rgb(0, 0, 0)
border是一个综合属性,不会在全部浏览器中都有返回值,但computedStyle.borderLeftWidth则会返回值;IE不支持getComputedStyle, 但还有一个currentStyle属性,返回CSSStyleDeclaration的实例
let myDiv = document.getElementById('myDiv') let computedStyle = myDiv.currentStyle' console.log(computedStyle.backgroundColor) // rgba(255, 0, 0) console.log(computedStyle.width) // 100px console.log(computedStyle.height) // 200px console.log(computedStyle.border) // undefined
2.2.2 操做样式表
1) CSS 规则
CSSStyleRule对象的属性
cssText: 返回整条规则对应的文本
selectorText: 返回当前规则的选择符文本, Firefox, Safari, Chrome和IE只读,Opera容许修改
style: 经过它设置和取得规则中特定的样式表(就像元素上的style属性同样)
let sheet = document.styleSheets[0] let rules = sheet.cssRules || sheet.rules let rule = rules[0] console.log(rule.selectorText) // #myDiv console.log(rule.style.cssText) // width: 100px; height: 100px; background-color: red; console.log(rule.style.backgroundColor) // red console.log(rule.style.width) // 100px console.log(rule.style.height) // 100px
2) 元素大小