DOM的用途:—— 使用JavaScript增删改查HTML文档内容

(1)节点树、元素树

(2)遍历HTML文档,查看其内容

(3)查找节点  

getElementById()

getElementsByTagName()

getElementsByName()

getElementsByClassName()

querySelector()

querySelectorAll()

document.documentElement

document.head

document.body

(4)获取/修改元素中HTML内容  innerHTML

(5)获取/修改元素的属性   setAttribute()

(6)新建节点,追加到HTML文档中

var element = document.createElement('tr');

var txt = document.createTextNode('新闻标题');

var comment = document.createComment('注释内容');

var attr = document.createAttribute('属性名');

(7)删除节点,将HTML文档中的元素删除

parentNode.removeChild( child );

(8)替换节点,将HTML文档中的某个元素删除,同一位置上再添加另一个元素

parentNode.replaceChild( newChild, existingChild );

(9)节点的新添  

parent.appendChild( newChild ) 

parent.insertBefore( newChild, existingChild )

 

原文地址:https://www.cnblogs.com/lengkafei/p/5605018.html