强制在IE中重新应用样式表 [英] Forcing to re-apply a style sheet in IE
问题描述
请考虑这个HTML:
<ul>
<li>first</li>
<li>second</li>
<li>third</li>
</ul>
此CSS:
li:first-child { background: yellow; }
和此Javascript(jQuery):
And this Javascript (jQuery):
$("ul").append($("li:first-child"));
http://fiddle.jshell.net/Xtuaf/
显然位于 Internet Explorer (8):first-child
在DOM更改时断开。我在Javascript中写了一个这个问题的解决方法(做样式; z-index
在我的case)。这是一个丑陋的快速修复,我想摆脱。更好的解决方案是强制在DOM更改时重新应用样式表。这可以做吗?
Apparently in Internet Explorer (8) :first-child
breaks when the DOM has changed. I wrote a workaround for this problem in Javascript (doing styling there; z-index
es in my case). It's an ugly quick fix that I want to get rid of. A better solution would be forcing to re-apply the style sheet when the DOM changes. Can this be done? Or is there an other solution to this problem?
推荐答案
在Internet Explorer中,可以通过设置类名来触发回流:
In Internet Explorer, a reflow can be triggered by setting the class name:
$("ul").append($("li:first-child")).addClass('x').removeClass('x');
或只是(demo: http://fiddle.jshell.net/Xtuaf/1/ ):
Or just (demo: http://fiddle.jshell.net/Xtuaf/1/):
$("ul").append($("li:first-child"))[0].className += '';
这篇关于强制在IE中重新应用样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!