javascript - 从 js 添加媒体查询

标签 javascript css

我有一个页面,我可以像这样打开一个新窗口

var myWindow = window.open("", "", "width=755.9100,height=347.7200");
myWindow.document.body.append(img);

我想从上一个窗口向这个新窗口添加一些样式

@page {size: A4;margin: 0;@media print {html, body {width: 210mm;height: 297mm;}

如何通过js将此添加到html中的head标签中?

最佳答案

window.matchMedia js中的函数:

用法很简单:

if (window.matchMedia("(min-width: 400px)").matches) {
    /* the viewport is at least 400 pixels wide */
} else {
    /* the viewport is less than 400 pixels wide */
}

一篇更有用的文章是here .

关于javascript - 从 js 添加媒体查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42572840/

相关文章:

javascript - 是否可以在浏览器中拦截第三方 POST 请求?

javascript - document.write来自mysql数据库的javascript并处理转义

html - 显示 : table-cell; in ie6/7 workaround

html - CSS3 : border on a border-radius div

JavaScript 按字符串中的数字排序

检查元音的 JavaScript 函数

javascript - 从 phantomjs 打印 pdf 时出现空格

php - 使用 include 函数时如何设置正确的路径

javascript - 在 yii 中提交 ajax 表单时遇到问题

html - Bootstrap Navbar-brand换色调试