10.1.- 其他 类型

一、Text 类型

1)基本信息

nodeType 3
nodeName #text
nodeValue 节点包含的文本
parentNode Element
childNodes 不支持(没有)

操作节点中文本的方法

节点中的文本可以使用nodeValue或data属性来获取

textNode.nodeValue = ''; //对文本节点进行内容的修改
textNode.data = ''; //对文本节点进行内容的修改
appendData(text) //将text文本添加到节点的末尾
deleteData(offset,count) //删除从offset开始的count个字符
insertData(offset,text) //在offset的指定的位置插入text
replaceData(offset,count,text) 
//用text替换从offset开始的count个字符
splitText(offset) //以offset为界线将当前文本节点分为两个文本节点
substringData(offset,count) //从offset开始提取count个字符

另外,存在一个length属性

2)创建文本节点

var textNode = document.createTextNode(text); 
//参数为节点的文本内容

3)规范化文本节点

element.normalize(); 
//在包含多个文本节点的父元素中调用,以合并文本节点

4)分割文本节点

var newNode = parentNode.splitText(offset);
//原节点从offset的位置处开始分裂
//0-offset的位置属于原节点
//返回的新节点为offset-末尾的字符
//offset位置处的字符属于新节点

二、Comment 类型

1)基本信息

nodeType 8
nodeName #comment
nodeValue 注释的内容
parentNode Document 或 Element
childNodes 不支持(没有)

操作节点的方法与文本节点类似,只是少了splitText方法

2)创建注释节点

var comment = document.createComment('a comment');

三、CDATASection 类型

1)基本信息

扫描二维码关注公众号,回复: 8614991 查看本文章

主要用于XML文档中,目前主流浏览器无法正确解析该节点

nodeType 4
nodeName #cdata-section
nodeValue CDATA区域中的内容
parentNode Document 或 Element
childNodes 不支持(没有)

操作节点的方法与文本节点类似,只是少了splitText方法

2)创建注释节点

var comment = document.createCDATASection(); 
//XML文档中使用

四、DocumentType 类型

nodeType 10
nodeName doctype的名称
nodeValue null
parentNode Document
childNodes 不支持(没有)

五、DocumentFragment 类型

1)基本信息

nodeType 11
nodeName #document-fragment
nodeValue null
parentNode null
childNodes 可以是Element、ProcessingInstruction、Comment、Text、CDATASection 或 EntityReference

2)创建文档片段

var fragment = document.createDocumentFragment(); 

六、Attr 类型

发布了43 篇原创文章 · 获赞 0 · 访问量 276

猜你喜欢

转载自blog.csdn.net/weixin_44774877/article/details/103984141