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

108
Visualizações
how to apply body.addClass("className") to iframe from the parent document

I have a WP page with embedded iframes. There is a widhet that changes the style of the page, and I need that it also changes the style of all embedded iframes. The style changes by clicking the button that has the following event handler:

NightVersionButton.click(function (event) {
    // this is a part that changes the page style (and it works)
    event.preventDefault();
    Cookies.set('contrast', 'night', { expires: 7, path: window.cookiePath });
    body.removeClass('highcontrast highcontrast2 highcontrast3');
    body.addClass('night');
    
    // this is a part that should change the style of all iframes, but it does not work due to the error "Uncaught TypeError: single_iframe.contentDocument.body.addClass is not a function"
    var all_iframes = document.getElementsByTagName('iframe');
    for (let single_iframe of all_iframes) {
        var iframe_doc = single_iframe.contentWindow ? single_iframe.contentWindow.document : single_iframe.contentDocument;
        iframe_doc.body.addClass('night');
        //$(single_iframe).contents().find('body').addClass('night'); // this jQuery alternative does not work neither
    }

});

However, the part of the JS code responsible for changing the styles in all iframes does not work, with the console error "Uncaught TypeError: single_iframe.contentDocument.body.addClass is not a function".

Please could you suggest the correct solution on how to change the JS that it will also apply addClass to all iframes?

about 4 years ago · Juan Pablo Isaza
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