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

207
Views
¿Cómo puedo eliminar un bloque <style> en línea anónimo que se inyecta con un widget?

Estoy creando un sitio que usa un widget de JavaScript para incrustar un formulario simple en la página:

 <script src="//example.com/formWidget.js?id=1234"></script>

Esto luego inyecta el formulario HTML directamente en la página para que podamos diseñarlo para que se adapte al sitio. Sin embargo, la persona que creó el formulario incluyó muchos estilos en línea que anulan el CSS del sitio. No quiero hacer que todas las reglas de CSS sean !important porque eso suele ser una mala idea ; y es importante tener en cuenta que el estilo en línea NO se realiza por elemento (por ejemplo <span style="border: 1px solid red;"> , que se resuelve en preguntas como esta ); pero como un bloque <style> que se inyecta en la página junto con el HTML:

 <style> .widget-form button { border: 1px solid red; float: left; } .widget-form input { background: green; } </style>

El bloque de style no tiene una ID a la que pueda apuntar selectivamente, entonces, ¿cómo puedo eliminar este bloque de estilo en línea inyectado? Estoy bien usando JS/jQuery para resolverlo, ya que así es como se agrega el widget.

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

0

Siempre que el bloque <style> contenga algo identificable de forma única, entonces es relativamente fácil de eliminar. Usando jQuery para recorrer todos los bloques <style> en la página, luego puede verificar su contenido para el selector infractor y eliminar el bloque completo:

 $('style').each(function(index, elem) { let styleBlock = $(this); let content = styleBlock.text(); if (content.indexOf('.widget-form') > -1) { // Found an inline style block - let's kill it! styleBlock.remove(); } });

También puede hacer esto de una manera muy similar con JS simple:

 var styles = document.querySelectorAll('style'); Array.prototype.forEach.call(styles, function(el, i) { let content = el.textContent; if (content.indexOf('.widget-form') > -1) { // Found an inline style block - let's kill it! el.remove(); // Or el.parentNode.removeChild(el); for maximum backwards compatibility } });
about 4 years ago · Juan Pablo Isaza Report

0

solo ponlo en tu archivo css

 .widget-form button { border: unset; float: unset; } .widget-form input { background: unset; }

agregue !importante si es necesario

about 4 years ago · Juan Pablo Isaza Report

0

entonces prueba esto

 $( "<html>*/</html>" ).appendTo( "style:last-of-type" ); $( "<html>/*</html>" ).prependTo( "style:last-of-type" );

el selector cambia según su archivo de índice

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!