如何获得HTML树的#Text节点数组

发布于 2025-01-29 06:05:53 字数 274 浏览 4 评论 0原文

我需要将HTML主体的所有#Text元素作为数组。 丰富的文本可以具有各种层次,因此我需要达到最低元素。 例如,对于下面的文本,我希望有8个元素的数组。

获取#文本节点的名称或标签或方法是什么?

I need to use all the #text elements of an html body as an array.
The rich text can have various levels so I need to get to the lowest element.
For example for the text below I'm expecting to have an array of 8 elements.

enter image description here

What is the name or tag or method to get the # text node?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

塔塔猫 2025-02-05 06:05:53

您可以通过节点递归扫描并将文本节点推入数组。

const textNodes = []

function pushTextNode(node) {
  if (node.nodeName === "#text") {
    const nodeVal = node.nodeValue.trim();
    if (nodeVal) {
      textNodes.push(nodeVal);
    }
    return;
  }
  node.childNodes.forEach((childNode) => {
    pushTextNode(childNode)
  });
}

pushTextNode(document.querySelector("#root"));
console.log(textNodes);
<div id="root">
  <span>
    0
    <b>
      12<u>3</u>
    </b>
    <u>
      4<b>5</b>
    </u>
    <b>67</b>8<a href="#">9</a>
  </span>
</div>

You can recursively scan through the nodes and push the text nodes into an array.

const textNodes = []

function pushTextNode(node) {
  if (node.nodeName === "#text") {
    const nodeVal = node.nodeValue.trim();
    if (nodeVal) {
      textNodes.push(nodeVal);
    }
    return;
  }
  node.childNodes.forEach((childNode) => {
    pushTextNode(childNode)
  });
}

pushTextNode(document.querySelector("#root"));
console.log(textNodes);
<div id="root">
  <span>
    0
    <b>
      12<u>3</u>
    </b>
    <u>
      4<b>5</b>
    </u>
    <b>67</b>8<a href="#">9</a>
  </span>
</div>

不疑不惑不回忆 2025-02-05 06:05:53

您需要指定第一个父标签并使用InnerText属性。

<script>
var text = document.getElementsByTagName("body")[0].innerText;
console.log(text.replace(/(\r\n|\n|\r|\t|\s)/gm, ''));
</script>

或者,如果您想使用jQuery,则可以这样做。

console.log($("body span").text().replace(/(\r\n|\n|\r|\t)/gm, ''));

You need to specify the first parent tag and use innerText attribute.

<script>
var text = document.getElementsByTagName("body")[0].innerText;
console.log(text.replace(/(\r\n|\n|\r|\t|\s)/gm, ''));
</script>

or if you want to use jquery , you can do like this.

console.log($("body span").text().replace(/(\r\n|\n|\r|\t)/gm, ''));
抱着落日 2025-02-05 06:05:53
//find the textnode like this//
const textNodes=document.querySelector("content");
//[put on a variable]//


//using this statement //
Array.from(textNodes).map((content)=>{
  //now add eventlistener//
  content.addEventListener("//event type//",functionHandler);
});
function functionHandler(e){
  //do anything what you need//
}
//find the textnode like this//
const textNodes=document.querySelector("content");
//[put on a variable]//


//using this statement //
Array.from(textNodes).map((content)=>{
  //now add eventlistener//
  content.addEventListener("//event type//",functionHandler);
});
function functionHandler(e){
  //do anything what you need//
}
慕巷 2025-02-05 06:05:53

最简单的方法是使用XPath,以下表达式返回给您所有具有文本的节点,

//*[string-length(text())>0]

如果您只想获取所有文本,请使用以下内容

//text()

The easiest way is to use Xpath, the following expression return to you all the nodes that have text

//*[string-length(text())>0]

if you just want to get all the text, use the following

//text()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文