我有一个DOM节点如何在其中获取其文本

时间:2022-10-15 07:26:54

I have a DOM node, lets say node and this may have some children and has text in it. I need to get only the text from it.

我有一个DOM节点,比方说节点,这可能有一些孩子,并有文本。我只需要从中获取文本。

I know that node.innerHTML gives the whole data between the tags but I don't need the child elements. This can be done using jquery once i get the node's id How to get text only from the DIV when it has child elements with text using jQuery? But in that case I am again finding the node which is a waste of time. Right now I already have the node and I only need to get its text. I tried node.text but it is returning undefined value. Please help.

我知道node.innerHTML给出了标签之间的整个数据,但我不需要子元素。一旦我得到节点的id,这可以使用jquery完成当如何使用jQuery使用带有文本的子元素时,如何仅从DIV获取文本?但在那种情况下,我再次找到节点,这是浪费时间。现在我已经有了节点,我只需要获取它的文本。我尝试了node.text但它返回了未定义的值。请帮忙。

2 个解决方案

#1


3  

Looping over the .childNodes and grabbing the .nodeValue of the text nodes should work:

循环遍历.chi​​ldNodes并获取文本节点的.nodeValue应该可以正常工作:

var foo = document.getElementById('foo'),
    txt = '';

[].forEach.call(foo.childNodes, function (subNode) {
  if (subNode.nodeType === 3) {
    txt += subNode.nodeValue;
  }
});

console.log(txt);

jsBin

jsBin

.childNodes is a NodeList, not an array. You cannot use array methods on them, foo.childNodes.forEach() would not work.

.childNodes是NodeList,而不是数组。你不能在它们上使用数组方法,foo.childNodes.forEach()是行不通的。

NodeList objects are however array-like objects, so we can use Function.prototype.call to treat foo.childNodes as if it were a real array and call Array.prototype.forEach on it.

然而,NodeList对象是类似于数组的对象,因此我们可以使用Function.prototype.call来处理foo.childNodes,就好像它是一个真正的数组并在其上调用Array.prototype.forEach。

The callback we provide .forEach checks the .nodeType of each Node, if it is a text node (a Node with a .nodeType of 3) we append it's value to our output buffer.

我们提供的回调.forEach检查每个Node的.nodeType,如果它是一个文本节点(.nodeType为3的Node),我们将它的值附加到我们的输出缓冲区。

#2


-1  

I am not sure if I understand correctly but if you need the text for each element of the node then for JQuery you would do:

我不确定我是否理解正确,但如果您需要节点的每个元素的文本,那么对于JQuery,您会这样做:

node.each(function (index){
console.debug($(this).text());
});

#1


3  

Looping over the .childNodes and grabbing the .nodeValue of the text nodes should work:

循环遍历.chi​​ldNodes并获取文本节点的.nodeValue应该可以正常工作:

var foo = document.getElementById('foo'),
    txt = '';

[].forEach.call(foo.childNodes, function (subNode) {
  if (subNode.nodeType === 3) {
    txt += subNode.nodeValue;
  }
});

console.log(txt);

jsBin

jsBin

.childNodes is a NodeList, not an array. You cannot use array methods on them, foo.childNodes.forEach() would not work.

.childNodes是NodeList,而不是数组。你不能在它们上使用数组方法,foo.childNodes.forEach()是行不通的。

NodeList objects are however array-like objects, so we can use Function.prototype.call to treat foo.childNodes as if it were a real array and call Array.prototype.forEach on it.

然而,NodeList对象是类似于数组的对象,因此我们可以使用Function.prototype.call来处理foo.childNodes,就好像它是一个真正的数组并在其上调用Array.prototype.forEach。

The callback we provide .forEach checks the .nodeType of each Node, if it is a text node (a Node with a .nodeType of 3) we append it's value to our output buffer.

我们提供的回调.forEach检查每个Node的.nodeType,如果它是一个文本节点(.nodeType为3的Node),我们将它的值附加到我们的输出缓冲区。

#2


-1  

I am not sure if I understand correctly but if you need the text for each element of the node then for JQuery you would do:

我不确定我是否理解正确,但如果您需要节点的每个元素的文本,那么对于JQuery,您会这样做:

node.each(function (index){
console.debug($(this).text());
});