How do I most accurately force the browser to recalculate styles, either on the whole body or on a single element?
I can't simply reload the whole DOM by replacing the innerHTML
or something, because that would lose all attached event handlers, so I'd need to use something that would just force the browser to recalculate the styles. This does need to work with IE8.
I have tried searching for anything that would fix this problem, but atleast with the term "recalculating styles" I don't get anywhere, and the only related result I could find for "reloading styles" was "How to refresh the style of a page after css switch". That question doesn't work for me though, since I am not directly changing the class of the element that needs to change styles, but its sibling.
Here is a simplified demo, with just the relevant code:
HTML:
<span id="foo">This gets a class,</span>
<div>...and this should become red,</div>
<button onclick="document.getElementById('foo').className = 'foo';">...if you click here</button>
CSS:
span.foo + div {color:red;}
If you test that demo, atleast in IE8-emulation mode in IE11, you'll see that the style won't update. For me, it only updates after I've selected the node in the DOM viewer in the F12 interface. (PS: IE8-emulation mode is in the bottom tab in IE11's F12 interface).