I tried the clock to get the results, but could not, below, I will publish everything that I have done, I hope that I can get some tips, BTW, Thank you.
from the error message, yes. This cssRules is null, definitely an error!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="index.css">
<title>css style</title>
<style type="text/css">
#demo {
font-size: 10px;
}
p {
border: 1px solid green;
}
</style>
</head>
<body>
<div id="demo" style="color: red;">
<p>how to access of document css!</p>
</div>
</body>
external css
#demo {
font-weight: 900;
}
p {
padding: 20px;
}
Javascript
<script>
var cssInline = document.getElementById('demo').style;
var cssInText = cssInline.cssText, cssColor = cssInline.color;
console.log(cssInText, cssColor);
var cssInHeada = document.getElementById('demo');
var cssHeadText = getComputedStyle(cssInHeada, null);
console.log(cssHeadText);
var cssInHeadb = document.getElementsByTagName('style')[0];
console.log(cssInHeadb.textContent);
var cssInHeadc = document.styleSheets[1];
console.log(cssInHeadc.cssRules[0].cssText);
var cssExtenal = document.styleSheets[0];
console.log(cssExtenal.cssRules[0].cssText);
</script>
Thanks guys!
source
share