Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
¿Cómo buscar recursivamente todos los elementos SPAN dentro del DIV?

Estoy tratando de encontrar todos los elementos SPAN que están dentro del DIV, y la función devuelve una matriz del SPAN encontrado.

A continuación hice una función recursiva, pero trae 43 SPAN, esperaba 7 SPAN:

 function findRecursive(myElement, elementName, element) { if (element == null){ element = {'element':null, 'array':[]} } if (element['element']) { myElement = element['element'] } if (element['array'] === undefined) { element['array'] = [] } var arr = element['array'] var els = myElement.getElementsByTagName(elementName) for (let i = 0; i < els.length; i++) { const e = els[i]; if (e.tagName == elementName) { arr.push(e) } args = {'element':e, 'array': arr} findRecursive(null, elementName, args) } return arr } var e = document.getElementById('main') var result = findRecursive(e, 'SPAN') console.log(result)
 <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div id="main"> <span> <span> <span> <span> <span> mySpan1 </span> </span> </span> <span> <span> mySpan2 </span> </span> </span> </span> </div> </body> </html>

¿Qué estoy mal con este código? Debería traer 7 SPAN

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Creo que lo has exagerado... ;-)

document.querySelectorAll('div[id="main"] span');

Más información sobre querySelector y querySelectorAll

about 4 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 Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!