@GivenCui
        
        2016-12-12T10:36:34.000000Z
        字数 2310
        阅读 851
    DOM
doucment instanceof Node
* 处为
<div class = "test">123</div>
| * | nodeType | nodeName | nodeValue | 
|---|---|---|---|
| 元素节点 | 1 | "DIV" | null | 
| 属性节点 | 2 | "class" | "test" | 
| 文本节点 | 3 | "#test" | "123" | 
<div class = "test">123</div>1. 元素节点nodeType -> 1nodeName --> "DIV"nodeValue -> null2. 属性节点 (~.getAttributeNode("class") || ~.attributes[idx])nodeType -> 2nodeName -> "class"nodeValue -> "test"3. 文本节点 (~.firstChild)nodeType -> 3nodeName -> "#text"nodeValue -> "123"
[ 注意 ] 换行符号算一个空的文本节点
| 项目 | 包含文本节点 | 只包含元素节点 | 
|---|---|---|
| 父节点 | parentNode | parentNode | 
| 子节点 | childNodes | children | 
| 第一个节点 | firstChild | firstElementChild | 
| 最后一个节点 | lastChild | lastElementChild | 
| 上一个兄弟节点 | previousSibling | previousElementSibling | 
| 下一个兄弟节点 | nextSibling | nextElementSibling | 
| 所有属性节点 | attributes | attributes | 
| 具体属性节点 | getAttribute("") | getAttribute("") | 
p = parentNode; xx=待操作节点
p.hasChildNodes()  // true || falsep.appendChild(xx)p.insertBefore(xx, 某节点)p.replaceChild(xx, 某节点)p.removeChild(xx)p.cloneNode(true || false)  // 深复制,浅复制
// 实现网站标题滚动效果var text=document.titlevar timerIDfunction newtext() {clearTimeout(timerID)text=text.substring(1,text.length)+text.substring(0,1)document.title = text;console.log(text);timerID = setTimeout("newtext()", 100)};newtext()
document.createElement() // 参数为tagName , e.g. "div"
if(ele.tagName.toLowerCase() == "div"){// 适用HTML和XML}返回对象  xx.style.cssText = "color:red" xx.style.color = "red"
xx.getAttribute()的注意事项 (IE7中和IE8中返回的就不同)1. xx.style 与 xx.getAttribute("style")不同,xx.style返回对象 xx.style.fontSize = "18px";xx.getAttribute("style") 返回字符串2. xx.onclick 与 xx.getAttribute("onclick")不同xx.onclick返回js函数xx.getAttribute返回字符串// 举例添加自定义属性data-index = "1"// dom的属性节点的操作, 都是字符串xx.setAttribute("data-index","1"); // 能在html中看见xx.getAttribute("data-index") // "1"// dom对象, typeof xx --> object, 本质是给object添加属性xx["data-index"] = 1xx["data-index"] // 1[总结]:1. 自定义属性应该用getAttribute或setAttribute读写2. 实际工作中用jq的$.data()方法
