[英]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
属性并检查是否应用了该属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.