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

149
Visualizações
Document.write no funciona correctamente con consultas de medios. ¿Qué puedo hacer? ¿O hay alguna alternativa?

Esta pequeña función me da dolor de cabeza.

document.write en combinación con consultas de medios simplemente no funcionará de manera receptiva.

Estoy tratando de cambiar la hoja de estilo css con document.write tan pronto como cambie max-width .

Funciona, pero no responde: tengo que volver a cargar la página.

Funciona perfectamente bien con document.body.style.backgroundColor como ejemplo, pero no con document.write .

¿Alguien sabe por qué, o hay una mejor manera?

 let Swidth = window.matchMedia("(max-width: 700px)") function reSize(Swidth) { if (Swidth.matches) { /* document.body.style.backgroundColor = "yellow"; // This works */ document.write('<link href="./src/css/variant1.css" type="text/css" rel="stylesheet">'); } else { /* document.body.style.backgroundColor = "blue"; // This works */ document.write('<link href="./src/css/variant2.css" type="text/css" rel="stylesheet">') } } reSize(Swidth); Swidth.addEventListener("change", reSize);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Puede utilizar el atributo media del elemento <link> .

 <link href="./src/css/variant1.css" type="text/css" rel="stylesheet" media="screen and (max-width: 700px)"> <link href="./src/css/variant2.css" type="text/css" rel="stylesheet" media="not screen and (max-width: 700px)">
about 4 years ago · Juan Pablo Isaza Relatório

0

Esta solución funciona para mí.

 function addStylesheet(theHref) { if (document.createStyleSheet) { document.createStyleSheet(theHref); } else { var newSheet = document.createElement('link'); newSheet.setAttribute('rel', 'stylesheet'); newSheet.setAttribute('type', 'text/css'); newSheet.setAttribute('href', theHref); document.getElementsByTagName('head')[0].appendChild(newSheet); } } let Swidth = window.matchMedia("(max-width: 700px)") function reSize(Swidth) { if (Swidth.matches) { document.body.style.backgroundColor = "yellow"; addStylesheet("./src/css/variant1.css") } else { document.body.style.backgroundColor = "blue"; addStylesheet("./src/css/variant2.css") } } reSize(Swidth); Swidth.addEventListener("change", reSize);
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