1
2
// get display: setting of class .button
var foo = document.css.getClass(".button").display;
var elems = document.getElementsByClassName("button");
Uncaught DOMException: CSSStyleSheet.cssRules getter: Not allowed to access cross-origin stylesheet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
function get_Button_display_style() { var list = document.styleSheets; // w3.css must be loaded first, so we get the correct rules here var rules = list[0].cssRules; for ( var i=0; i<rules.length; i++ ) { var text = rules[i].cssText; // XXX: much too complex code for getting the display: definition of // a css class // if it is the rule for w3-button if ( text.indexOf(".w3-button ") > -1 ) { // which includes a display: definition if ( text.indexOf("display:") > 0 ) { // and is not part of the @media definition if ( text.indexOf("@media" ) == -1 ) { // return display: definition return rules[i].style.display; } } } } }
1
2
3
const btn = document.getElementById('btn');
const display = window.getComputedStyle(btn).display;
console.log(display);
<button id="btn" class="w3-button andere-Klasse" style="display:anderer-nicht-mehr-relevanter-Wert">foo</button>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="w3.css">
</head>
<body>
<div class="w3-button">Testing</div>
<button onclick="change()">Change</button>
<button onclick="ask()">Ask</button>
<script type="text/javascript">
function change() {
document.querySelector('.w3-button').style.display = "block";
}
function ask() {
console.log("from file: " + get_Button_display_style("w3.css", ".w3-button", "display"))
console.log("style: " + window.getComputedStyle(document.querySelector('.w3-button'),null).getPropertyValue('display'))
}
function getStyleSheet(unique_title) {
for(var i = 0; i < document.styleSheets.length; i++) {
var sheet = document.styleSheets[i];
if(sheet.href.indexOf(unique_title) > -1) {
return sheet;
}
}
}
function get_Button_display_style(unique_title, class_id_name, attribute) {
var stylesheet = getStyleSheet(unique_title)
if(stylesheet) {
var rules = stylesheet.cssRules;
for ( var i=0; i<rules.length; i++ ) {
var text = rules[i].cssText;
if ( text.indexOf(class_id_name) > -1 ) {
if ( text.indexOf(attribute + ":") > 0 ) {
if ( text.indexOf("@media" ) == -1 ) {
return rules[i].style.display;
}
}
}
}
}
}
</script>
</body>
</html>