This article is more JavaScript standard reference tutorial - Ruan Yifeng finishing notes
Text node
Text nodes represent the textual content of element nodes and attribute nodes. We can usually get text nodes using properties such as the parent node's firstChild
, nextSibling
etc.
Text nodes can be created using document.createTextNode()
methods
Browsers provide a native Text
constructor that returns a text node instance
// 空字符串
var text1 = new Text(); //""
// 非空字符串
var text2 = new Text('This is a text node');
//"This is a text node"
The empty string is also a text node
Text node properties
data
data
Properties are equivalent to nodeValue
properties, used to set or get the content of the text node
//<div id="msg">你好</div>
// 读取文本内容
document.querySelector('div').firstChild.data //"你好"
// 等同于
document.querySelector('div').firstChild.nodeValue//"你好"
// 设置文本内容
document.querySelector('div').firstChild.data = 'Hello World';
//<div id="msg">Hello World</div>
wholeText
wholeText
Returns the current text node and adjacent text nodes as a whole. Usually the same as data
and textContent
properties
Special case:
<p id="para">A <em>B</em> C</p>
var el = document.getElementById('para');
el.firstChild.wholeText // "A "
el.firstChild.data // "A "
<em>
If the node is removed , it wholeText
will be different from the attribute
el.removeChild(para.childNodes[1]);
el.firstChild.wholeText // "A C"
el.firstChild.data // "A "
other properties
length
Property returns the text length of the current text nodenextElementSibling
Returns the same-level element node behind the current text node, or returns if it cannot be retrievednull
previousElementSibling
The attribute returns the element node of the same level in front of the current text node, otherwise it returnsnull
Methods of Text text nodes
It has 5 commonly used methods
appendData()
:Text
Append a string to the end of the node.deleteData()
: DeleteText
the substring inside the node. The first parameter is the start position of the substring, and the second parameter is the length of the substring.insertData()
:Text
Insert a string at the node, the first parameter is the insertion position, and the second parameter is the inserted substring.replaceData()
: Used to replace text, the first parameter is the replacement start position, the second parameter is the length to be replaced, and the third parameter is the newly added string.subStringData()
: used to get the substring, the first parameter isText
the starting position of the substring in the node, and the second parameter is the length of the substring
// HTML 代码为
// <p>Hello World</p>
var pElementText = document.querySelector('p').firstChild;
pElementText.appendData('!');
// 页面显示 Hello World!
pElementText.deleteData(7, 5);
// 页面显示 Hello W
pElementText.insertData(7, 'Hello ');
// 页面显示 Hello WHello
pElementText.replaceData(7, 5, 'World');
// 页面显示 Hello WWorld
pElementText.substringData(7, 10);
// 页面显示不变,返回"World "
remove()
This method is used to remove the currentText
node
// <p>Hello World</p>
document.querySelector('p').firstChild.remove()
//删除后为: <p></p>
splitText()
The method splits the text node into two and becomes two adjacent nodes
// <p id="p">foobar</p>
var p = document.getElementById('p');
var textnode = p.firstChild;
var newText = textnode.splitText(3);
newText // "bar"
textnode // "foo"
- Parent elements can
normalize()
combine them by methods
p.childNodes.length // 2
// 将毗邻的两个 Text 节点合并
p.normalize();
p.childNodes.length // 1
DocumentFragment node
DocumentFragment
Nodes represent document fragments and are also a completeDOM
tree structure. But it has no parent node.- We can create it using
document.createDocumentFragment()
methods and methodsnew DocumentFragment()
DocumentFragment
A node can only insert its children into a document, it cannot itself.- Inserting the document of its child nodes can be used
appendChild()
,insertBefore()
etc., theDocumentFragment
node is used as the parameter of the function
DocumentFragment
The node object does not have its own properties and methods, it all inherits from Node
nodes and ParentNode
nodes. So it can also use the following four properties:
children
: Returns a dynamicHTMLCollection
collection object, includingDocumentFragment
all child element nodes of the current object.firstElementChild
: ReturnsDocumentFragment
the first child element node of the current object, or returns if there is nonenull
.lastElementChild
: ReturnsDocumentFragment
the last child element node of the current object, or returns if there is nonenull
.childElementCount
: Returns theDocumentFragment
number of all child elements of the current object.