相比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) 元素大小