[js] DOM操作

DOM

operation

e.appendChild(newNode) // append newNode at the last of e's child list, return it. if newNode is already one node in html, it would moved here 
e.insertBefore(newNode, positionedNode) // append newNode to position, null to last one
e.replaceChild(newNode, toBeReplacedNode)
e.removeChild(toBeRemovedNode)
e.cloneNode(boolean) // true to deep-clone, false to shallow-copy

create

document.createElement() // create new ele, not in flow now, must be append or write
document.createTextNode()
document.write(node) // write new node to flow // use this after onload would cover former document

change content

e.innerHTML = new HTML
e.attribute = new attr
e.style.property=new style

text
have no child

e.nodeName = '#text'
e.nodeValue // value, can be modified, and will auto transcode
e.normalize() // merge all text node
e.splitText(position) // split text at position

query

e.childNodes // array, all child
e.firstChild // first child node
e.firstElementChild // first ele child node 
e.lastChild
e.lastElementChild
e.parentNode
e.offsetParent // the nearest positioned parent ele
e.previousSibling // the previous one node
e.previousElementSibling // the previous ele node
e.nextSibling 
e.nextElementSibling

ele function

e.tagName or e.nodeName
e.getAttribute(attr) 
e.setAttribute(attr, value) 
e.removeAttribute(attr) 

tips
在IE8以上获取子节点

var arr = Array.prototype.slice.call(node.childNodes, 0);

lookme
1. display none & visibility hidden
first will remove from struction, no width and height, visibility only hide it. So display will cause reflow and both will cause redraw

2. window.onload & $(document).ready
ready occurs after the document has been loaded, onload occurs later, when all content(e.g. img) has been loaded.

3. html=document.documentElement

4. 动态script

document.write

var s = document.createElement("script");   
s.type = "text/javascript";   
s.src = "test.js";   
document.body.appendChild(s); 

5.动态css

**6.**DOM是动态查询的,所以最好减少使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值