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

210
Views
¿Por qué el tamaño de mi fuente no se ajusta con vanilla js?

Tengo este pequeño código aquí y, según los registros de la consola, puedo ver que aparentemente cambió, pero en la pantalla no cambia. ¿Qué podría estar saliendo mal?

JS vainilla abajo

 const gameItem = document.querySelectorAll('.game-item'); const gameItemS = document.querySelector('.game-item'); document.addEventListener('DOMContentLoaded', () => { titleSize(gameItem); }); const titleSize = (gameItem) => { [...gameItem].forEach((title) => { let textSize = window .getComputedStyle(title, null) .getPropertyValue('font-size'); let gameTitle = title.innerText; // console.log(gameTitle, gameTitle.length, textSize); if (gameTitle.length > 7) { textSize = '5px'; console.log( 'The font on this was changed: ' + gameTitle, gameTitle.length, textSize ); } }); };

html a continuación

 <section class="games"> <h1>Games</h1> <ol class="container"> <li class="game-item">Halo<img src="" /></li> <li class="game-item">Pokemon<img src="" /></li> <li class="game-item"> Animal Crossing<img src="" /> </li> <li class="game-item">Genshin Impact<img src="" /></li> <li class="game-item">Skyrim<img src="" /></li> </ol> </section>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Asignar a textSize solo cambia el valor de esa variable, no tiene ningún efecto en el elemento. Cuando let textSize = /*...*/.getPropertyValue('font-size'); , que simplemente copia el valor del objeto de estilo calculado en la variable. No hay un vínculo permanente entre la variable y ese objeto de estilo calculado. (Incluso si lo hubiera, la asignación al objeto de estilo calculado no cambiará el elemento).

Para cambiar el estilo del elemento, debe asignarlo a title.style.fontSize (o title.style["font-size"] ):

 const gameItem = document.querySelectorAll('.game-item'); const gameItemS = document.querySelector('.game-item'); document.addEventListener('DOMContentLoaded', () => { titleSize(gameItem); }); const titleSize = (gameItem) => { [...gameItem].forEach((title) => { let textSize = window .getComputedStyle(title, null) .getPropertyValue('font-size'); let gameTitle = title.innerText; // console.log(gameTitle, gameTitle.length, textSize); if (gameTitle.length > 7) { title.style.fontSize = '5px'; console.log( 'The font on this was changed: ' + gameTitle, gameTitle.length, textSize ); } }); };
 <section class="games"> <h1>Games</h1> <ol class="container"> <li class="game-item">Halo<img src="" /></li> <li class="game-item">Pokemon<img src="" /></li> <li class="game-item"> Animal Crossing<img src="" /> </li> <li class="game-item">Genshin Impact<img src="" /></li> <li class="game-item">Skyrim<img src="" /></li> </ol> </section>

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!