在工作偶尔会遇到改变伪元素样式的场景,虽然也有很多解决方法,
但是这个方法能让你像操作正常元素一样操作伪元素样式

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
function ruleSelector(selector) {
function uni(selector) {
return selector.replace(/::/g, ':')
}
// es6
return Array.from(document.styleSheets).reduce((a,b) => {
return a.concat(Array.from(b.cssRules))
}, []).filter(x => {
return uni(x.selectorText) === uni(selector);
})
// es5
// return Array.prototype.filter.call(Array.prototype.concat.apply([], Array.prototype.map.call(document.styleSheets, function (x) {
// return Array.prototype.slice.call(x.cssRules);
// })), function (x) {
// return uni(x.selectorText) === uni(selector);
// });
}

var toggle = false,
pseudo = ruleSelector("ref::before").slice(-1);

document.querySelector("article").onclick = function () {
pseudo.forEach(function (rule) {
if (toggle = !toggle)
rule.style.color = "red";
else
rule.style.color = "black";
});
}