Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript触发媒体查询_Javascript_Jquery_Css_Media Queries - Fatal编程技术网

使用javascript触发媒体查询

使用javascript触发媒体查询,javascript,jquery,css,media-queries,Javascript,Jquery,Css,Media Queries,当使用Javascript单击按钮时,是否可以触发媒体查询@media(最大宽度:600px){.a{…}?(浏览器宽度实际上不小于600px) /$('#b')。单击(函数(){//trigger the media query}) .a{背景色:绿色;} @介质(最大宽度:600px){ .a{背景色:黄色;/*许多其他规则*/} } 你好 不能使用JavaScript触发媒体查询样式,但可以使用单独的样式表并修改其media属性来启用它们。我的意思是: index.html <!

当使用Javascript单击按钮时,是否可以触发媒体查询
@media(最大宽度:600px){.a{…}
(浏览器宽度实际上不小于600px)

/$('#b')。单击(函数(){//trigger the media query})
.a{背景色:绿色;}
@介质(最大宽度:600px){
.a{背景色:黄色;/*许多其他规则*/}
}

你好

不能使用JavaScript触发媒体查询样式,但可以使用单独的样式表并修改其
media
属性来启用它们。我的意思是:

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" media="all" href="regular_styles.css">
    <link rel="stylesheet" media="(max-width: 600px)" href="small_screen.css" id="small">
</head>
<body>

    <div class="a">Hello</div>
    <input id="b" type="button" value="Change" />

    <script src="jquery.min.js"></script>
    <script>
        $('#b').click(function(){
            // set the media query perimeter to "all"
            $('#small').attr('media', 'all');
        });
    </script>
</body>
</html>
小屏幕。css

.a { background-color: green; }
.a { background-color: yellow; /* many other rules */ }