【发布时间】:2018-07-23 01:10:32
【问题描述】:
这是一个简单的问题示例:
<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>
myStyle.css body{background-color:green;}
该脚本适用于<style></style>
解决方案:
- 当文件在线/本地主机时工作。
- 适用于其他浏览器。 (即 Internet Explorer、Microsoft Edge、Firefox)
- chrome --allow-file-access-from-files
【问题讨论】:
-
开发工具中的网络选项卡说什么,样式表加载了吗?
-
样式表已加载。
-
那么控制台中有错误消息吗?
-
未捕获的 DOMException:无法从“CSSStyleSheet”读取“cssRules”属性:无法访问 window.onload 中的规则
-
您的浏览器仍然丢失,我的机器上安装的任何浏览器都无法重现该问题...甚至在 IE 中也无法重现。
标签: javascript html css google-chrome cors