javascript - 想通过js判断css的媒体查询 出错
问题描述
@media screen and (min-width: 360px) and (max-width: 375px){ .ejiao_description .form form p{margin-bottom: 11px; }}
css中媒体查询是这么写的js
var screen1 = window.matchMedia(’@media (min-width: 360px) and (max-width: 375px)’); if (screen1.matches){XXXXXX..... } else{XXXX.... }
但是现在没起作用 请问哪里错了么
问题解答
回答1:https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
请看文档
回答2:<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title> <style>@media screen and (min-width: 360px) and (max-width: 375px){ .aa .form form p{margin-bottom: 11px; }} </style></head><body><!-- 想通过js判断css的媒体查询 出错 --><p class='aa'> <p class='form'><form> <p>123123</p></form> </p></p></body><script> var screen1 = window.matchMedia(’(min-width: 360px) and (max-width: 375px)’); console.log( screen1 ); if (screen1.matches){console.log( 1 ); } else{console.log( 2 ); }</script></html>
为window.matchMedia(’(min-width: 360px) and (max-width: 375px)’);,没@media
相关文章:
1. android - NavigationView 的侧滑菜单中如何保存新增项(通过程序添加)2. angular.js - angular做点击购买时的遮罩层3. 请问这位老师的PHPSTORM主题是自定义的吗4. 冒昧问一下,我这php代码哪里出错了???5. 关于thinkphp 5.1中,ajax提交数据url的格式写法,加花括号就出错,请老师指点6. 老师 我是一个没有学过php语言的准毕业生 我希望您能帮我一下7. 请教!!!本地laravel项目我想本地运行,怎么在本地访问控制器里的方法。8. 连续的数值怎么用分组显示,求大神指导,求各位老师帮忙9. 有大佬知道这种接口文件怎么使用吗?10. AirPods Pro 2连接Pixel 7出问题:播放90秒自动断连
