使用代码检查此处的示例以列出正在应用于当前页面的媒体查询
JavaScript:
var mediaQueryCount = 0;
$(document).ready(function(){
$links = $('link[rel="stylesheet"]');
$.each($links, function(i, item){
if(item.media){
mediaQueryCount++;
var start = item.media.indexOf('(')+1;
var end = item.media.indexOf(')');
var str = item.media.substring(start, end);
var listItem = $('<li />',{
html: str.split(':')[1]
}).appendTo("#result");
}
});
$('#totalMedia').text(mediaQueryCount);
});
HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" media="only screen and (min-width: 30em)" />
<link rel="stylesheet" media="only screen and (min-width: 40em)"/>
<link rel="stylesheet" media="only screen and (min-width: 960px)"/>
</head>
<body>
<p>There are <span id="totalMedia"></span> media queries loaded on this page</p>
<ol id="result">
</ol>
</body>
</html>