您的位置:首页技术文章
文章详情页

javascript - 想通过js判断css的媒体查询 出错

【字号: 日期:2023-04-10 11:59:56浏览:43作者:猪猪

问题描述

@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:

javascript - 想通过js判断css的媒体查询 出错

回答3:

<!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

标签: JavaScript
相关文章: