According to this answer, I get all the element styles,
home.html
<div id="divClass" class="myclass">Text Here</div>
style.css
#divClass{
color:red;
background-color:black;
float:left;
}
script.js
function copyComputedStyle(a) {
var sheets = document.styleSheets,
o = {};
for (var i in sheets) {
var rules = sheets[i].rules || sheets[i].cssRules;
for (var r in rules) {
if (a.is(rules[r].selectorText)) {
o = $.extend(o, css2json(rules[r].style), css2json(a.attr('style')));
}
}
}
return o;
}
function css2json(css) {
var s = {};
var pattern = /^[1-9][0-9]?$|^100$/;
if (!css) return s;
if (css instanceof CSSStyleDeclaration) {
for (var i in css) {
if (css[i].toLowerCase) {
// Some of the key value pairs were vice versa, so try to make them in order!
// Ex: 0:flex-grow; 0:flex-shrink; --------> flex-grow: 0; flex-shrink: 0;
// End
if (pattern.test(css[i])) {
s[css[css[i]].toLowerCase()] = css[i];
} else {
s[css[i].toLowerCase()] = css[css[i]];
}
}
}
} else if (typeof css == 'string') {
css = css.split('; ');
for (var i in css) {
var l = css[i].split(': ');
s[l[0].toLowerCase()] = l[1];
}
}
return s;
}
console.log(copyComputedStyle($("#divClass")));
it returns an Object
{
background-color: "black",
color: "red",
float: "left",
left: ""
}
Now, when I add any pseudo-selector hover
or before
and after
to this div, the copyComputedStyle
does not log the added styles and return the same style as before!
#divClass{
color:red;
background-color:black;
float:left;
}
#divClass:hover{
color:blue;
}
How can I overcome that?