Лучшим решением было бы изменить стили с помощью класса. Обычно так работают темы. Вы устанавливаете класс для тела, который изменяет то, что вы хотите изменить.
window.setTimeout( function () {
document.body.classList.add("luckyGreen")
}, 4000)
window.setTimeout( function () {
document.body.classList.remove("luckyGreen")
}, 8000)
pre {
background-color: #CCC;
}
body.luckyGreen {
color:green;
}
body.luckyGreen pre {
background-color: #CFC;
}
Hello there
X
Hello there
Y
Hello there
Но по какой-то причине вы хотите изменить класс, вы можете написать новое css правило.
function addRule() {
var styleEl = document.createElement('style');
document.head.appendChild(styleEl);
var styleSheet = styleEl.sheet;
var ruleStr = "pre { background-color: red; }"
styleSheet.insertRule(ruleStr, styleSheet.cssRules.length);
}
window.setTimeout(addRule, 4000)
Hello there
X
Hello there
Y
Hello there