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

74
Views
Cambio de texto de párrafo a extensión estilizada

Estoy tratando de cambiar el texto del párrafo en un para cada ciclo. Esto parece un problema simple (¿específico de JS?). Soy nuevo en este lenguaje, vengo de Matlab y Java. Violín adjunto. Cualquier ayuda es apreciada.

https://jsfiddle.net/npkx3of4/1/

 function myFunction() { var ps = document.getElementsByClassName('.test'); for (var p of ps) { p = p.replace('a', '<span style="color:blue;">b</span>'); } alert("Working?"); return null; }
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

  1. getElementsByClassName('.test') debe ser getElementsByClassName('test') .

  2. Debería asignar el resultado de reemplazar el innerHTML del párrafo nuevamente al innerHTML del párrafo.

 function myFunction() { var ps = document.getElementsByClassName('test'); for (var p of ps) { p.innerHTML = p.innerHTML.replace('a', '<span style="color:blue;">b</span>'); } alert("Working?"); return null; }
 <h1>Test</h1> <hr> <p class="test">a</p> <button type="button" onclick='myFunction();'>Click Me!</button>

about 4 years ago · Santiago Trujillo Report

0

No es necesario agregar un punto antes del nombre de la clase cuando se usa getElementsByClassName .

 var ps = document.getElementsByClassName('test');

Seleccione solo el contenido de texto sin formato dentro del elemento, luego reemplace todos los caracteres especificados antes de volver a escribir el HTML interno.

 ... for (var p of ps) { p.innerHTML = p.textContent.replaceAll('a', '<span style="color:blue;">b</span>'); } ...
about 4 years ago · Santiago Trujillo 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!