Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

562
Vistas
Why does Node.childNodes not return all nodes in vuejs single file component?

From MDN

The Node.childNodes read-only property returns a live NodeList of child nodes of the given element where the first child node is assigned index 0. Child nodes include elements, text and comments.

Which works as expected

Jsfiddle

btn.addEventListener('click', ()=>{
    console.log(container.childNodes)
})
  <div id="container">
    <span>A</span>
    <span>B</span>
    <span>C</span>
  </div>
  <button id='btn'>Log Nodes</button>

0: "#text"
1: <span>
2: "#text"
3: ...
length: 7

This logs every node including text nodes, etc.

But when used in Vue it only logs elements without the text nodes inside.

1: <span>
2: <span>
3: <span>
length: 3

Codesandbox with vue

I know you're not suppose to access elements like this in Vue, but I'm wondering if this is normal behavior and why it happens. And how do I get all the nodes inside an element in Vue ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It is very tricky to compare normal static HTML and what Vue renders because it's "apples vs pears" comparison.

The template you give to a Vue may look like HTML but Vue don't use it as HTML text. It parses the template and produce render function which is just JavaScript code, that generates VNode's (virtual DOM)

And the thing is that during the parsing, Vue tends to ignore all insignificant whitespace by default.

There is a compiler setting that controls the whitespace handling. While default value condense removes most of the whitespace, value preserve will preserve some.

You can compare the difference in online Vue template explorer:

  1. condense
  2. preserve
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda