Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
How can I remove an anonymous inline <style> block being injected with a widget?

I'm building a site that uses a JavaScript widget to embed a simple form into the page:

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

This then injects the form HTML directly into the page for us to style to suit the site. However, the person who has made the form has included a lot of inline styling that's overriding the site's CSS. I don't want to make every CSS rule !important because that's usually a bad idea; and it's important to note that the inline styling is NOT done on a per-element basis (e.g. <span style="border: 1px solid red;">, which is solved in questions like this); but as a <style> block being injected into the page alongside the HTML:

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

.widget-form input {
  background: green;
}
</style>

The style block doesn't have an ID that I can selectively target, so how can I remove this injected inline style block? I'm fine using JS/jQuery to solve it, since that's how the widget is being added.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

As long as the <style> block contains something uniquely identifiable, then it's relatively straightforward to remove. Using jQuery to loop through all the <style> blocks in the page, you can then check their content for the offending selector and remove the entire block:

$('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();
    }
});

You can also do this in a very similar way with plain JS:

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 Relatório

0

just put it on your css file

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

.widget-form input {
  background: unset;
}

add !important if necessary

about 4 years ago · Juan Pablo Isaza Relatório

0

then try this

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

the selector changes according to your index file

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda