简体   繁体   English

如何找出应用了哪些样式表(通过javascript检查)?

[英]How find out which stylesheets were applied (check via javascript)?

If I have two stylesheets set with media queries: 如果我为媒体查询设置了两个样式表:

<link rel='stylesheet' media='only screen and (min-width: 64.1em)' href='desktop.css' />
<link rel='stylesheet' media='only screen and (max-width: 64em)' href='mobile.css' />

And I'm looking for a class in document.styleSheets , how would I be able to ignore the non-active stylesheet? 而且我正在document.styleSheets寻找一个类,如何能够忽略非活动样式表?

For example: 例如:

//Pseudo-code
for ( var i = 0; i < document.styelSheets.length; i++ ) if ( document.styelSheets[ i ].isApplied ) ...

Is there a way to know? 有没有办法知道?

There's a nice Js feature called window.matchMedia() , that can be used to test if a certain media query matches to the viewport, for example: 有一个很好的Js功能,称为window.matchMedia() ,可用于测试某个媒体查询是否与视口匹配,例如:

if(window.matchMedia('only screen and (min-width: 64.1em)').matches) {
    //match
}
else {
    //don't match
}

If you can iterate through all link tags, you could test each media attribute and check if it is applied. 如果可以遍历所有链接标记,则可以测试每个media属性并检查是否应用了该属性。

MDN .matchMedia() docs MDN .matchMedia()docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript如何找到按下了哪些按钮? - How can javascript find which buttons were pressed? 如何找出哪个JavaScript库拥有$ - How to find out which javascript library owns $ 如何找出要在JavaScript中删除的行 - How to find out which row to delete in javascript 如何找出在javascript中加载了哪些javascript? - How to find out which javascripts are loaded in javascript? 如何找出触发了哪些 JavaScript 事件? - How to find out which JavaScript events fired? 如何删除通过 Ajax 加载到 DOM 中的 querySelectorAll 匹配的所有元素的特定属性? (使用纯 JavaScript) - How to remove specific attributes of all elements matched by querySelectorAll which were loaded into DOM via Ajax? (with plain JavaScript) 要查找单击的两个图像中的哪个图像并将其报告给数据库 - To find which image out of the two were clicked and report it to Database 如何找出哪个签入快速验证器触发了错误 - how to find out which check in express validator is triggering the errors 控制通过JavaScript应用哪些CSS媒体查询 - Control which css media queries are applied via javascript 如何使用JavaScript检查事件是否应用于元素? - How to check if event is applied on an element with javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM