这是一个简单的问题示例:
<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)我想获得所有CSS声明的所有背景和颜色属性值.
例如:
body {
background: #fff;
}
.aaa {
background: #dedede;
color: #000
}
.text {
color: #333;
padding: 10px
}
Run Code Online (Sandbox Code Playgroud)
我想获得这样的输出,并且这些属性的值需要存储在数组中.
body {
background: #fff;
}
.aaa {
background: #dedede;
color: #000
}
.text {
color: #333;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用jQuery来做到这一点.我可以得到一个特定类的背景属性值,如.aaa或.text.我如何获得所有类的值?
$('#btn-invert').on('click', function() {
var color = $("body").css("background");
// var test = invertColor("#00a3fe");
console.log(color);
});
Run Code Online (Sandbox Code Playgroud)