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

196
Views
Javascript elimina el estilo sin darle una identificación o una clase

entonces tengo este código HTML donde escondo el cuerpo del sitio web usando

 <style> body{ display: none; } </style> <body> <p>Something..</p> </body>

pero ahora quiero eliminar esto usando Javascript sin darle al cuerpo una ID o una clase. Como solución, probé lo siguiente, pero no funciona.

 document.getElementsByTagName("body")[0].removeAttribute("style");

Cualquier ayuda es apreciada. Gracias.

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

0

Si desea eliminar las etiquetas de estilo agregadas en su HTML, debe seleccionarlo usando document.getElementsByTagName y llamar al método de remove a sus nodos.

violín de trabajo

 document.getElementsByTagName("style")[0].remove();
 body { display: none; }
 <p>Something..</p>

Si desea establecer un estilo para el elemento

Si solo desea configurar el estilo de display de la etiqueta del cuerpo, puede hacerlo con javascript usando.

 document.getElementsByTagName("body")[0].style.display

Solución más genérica

 const body = document.getElementsByTagName("body")[0]; const targetStyles = { display: 'block', } Object.entries(targetStyles).forEach(([style, value]) => body.style[style] = value);
 body { display: none; }
 <p>Something..</p>

¿Cuál es el problema con tu guión?

 document.getElementsByTagName("body")[0].removeAttribute("style");

Este script selecciona la etiqueta del body de HTML y elimina el estilo en línea agregado al cuerpo. Funcionará para la siguiente plantilla, pero no para sus requisitos.

 document.getElementsByTagName("body")[0].removeAttribute("style");
 <body style="background: blue;"> <p>Something..</p> </body>

about 4 years ago · Juan Pablo Isaza Report

0

Puede configurar la propiedad de estilo de display de document.body para unset :

 document.body.style.display = "unset"
 <style> body { display: none; } </style> <body> <p>Something..</p> </body>

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!