这是一个简单的问题示例:
<html>
<head>
<link rel='stylesheet' href='myStyle.css'>
<script>
window.onload=function(){
try{
alert(document.styleSheets[0]); // works
alert(document.styleSheets[0].cssRules); // doesn't even print undefined
}catch(e){alert(e)} // catch and alert the error
}
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
myStyle.css body{background-color:green;}
如果你使用脚本工作正常 <style></style>
解决方案:
1,作品文件时在线/本地主机
2.作品与其他浏览器(如IE浏览器,微软边缘,火狐)
3 铬--allow-文件访问从-文件
在Code.org的App Lab编辑器中,我们最近开始在Chrome 64中看到此错误:
Uncaught DOMException: Failed to read the 'rules' property from 'CSSStyleSheet'
Run Code Online (Sandbox Code Playgroud)
此函数中引发错误,旨在检测浏览器是否正在使用CSS媒体查询styleSheets[i].cssRules.
/**
* IE9 throws an exception when trying to access the media field of a stylesheet
*/
export function browserSupportsCssMedia() {
var styleSheets = document.styleSheets;
for (var i = 0; i < styleSheets.length; i++) {
var rules = styleSheets[i].cssRules || styleSheets[i].rules;
try {
if (rules.length > 0) {
// see if we can access media
rules[0].media;
}
} catch (e) {
return false;
} …Run Code Online (Sandbox Code Playgroud)