• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

161
Views
¿Cómo puedo obtener una matriz de nodos #text del árbol html?

Necesito usar todos los elementos #text de un cuerpo html como matriz. El texto enriquecido puede tener varios niveles, por lo que necesito llegar al elemento más bajo. Por ejemplo, para el texto a continuación, espero tener una matriz de 8 elementos.

ingrese la descripción de la imagen aquí

¿Cuál es el nombre, la etiqueta o el método para obtener el nodo de texto #?

almost 3 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede escanear recursivamente a través de los nodos y empujar los nodos de texto en una matriz.

 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>

almost 3 years ago · Juan Pablo Isaza Report

0

Debe especificar la primera etiqueta principal y usar el atributo innerText.

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

o si quieres usar jquery, puedes hacer esto.

 console.log($("body span").text().replace(/(\r\n|\n|\r|\t)/gm, ''));
almost 3 years ago · Juan Pablo Isaza Report

0

//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// }
almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error